diff --git a/Sources/Glide/Localizable.xcstrings b/Sources/Glide/Localizable.xcstrings index d9fff10..44b9621 100644 --- a/Sources/Glide/Localizable.xcstrings +++ b/Sources/Glide/Localizable.xcstrings @@ -236,6 +236,59 @@ } } }, + "%@ has a minimum width of %lld px, so Glide can't make it narrower." : { + "extractionState" : "manual", + "localizations" : { + "de" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ hat eine Mindestbreite von %lld px, daher kann Glide sie nicht schmaler machen." + } + }, + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ has a minimum width of %lld px, so Glide can't make it narrower." + } + }, + "es" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ tiene un ancho mínimo de %lld px, así que Glide no puede hacerla más estrecha." + } + }, + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ a une largeur minimale de %lld px, donc Glide ne peut pas la rendre plus étroite." + } + }, + "ja" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ の最小幅は %lld px のため、Glide では幅を狭くできません。" + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@의 최소 너비는 %lld px이므로 Glide에서는 더 좁힐 수 없습니다." + } + }, + "pt-BR" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ tem largura mínima de %lld px, então o Glide não consegue deixá-la mais estreita." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ 的最小宽度为 %lld px,因此 Glide 无法将其变窄。" + } + } + } + }, "%lld columns" : { "extractionState" : "manual", "localizations" : { diff --git a/Sources/Glide/WindowManager.swift b/Sources/Glide/WindowManager.swift index 2d1d3c6..0e963db 100644 --- a/Sources/Glide/WindowManager.swift +++ b/Sources/Glide/WindowManager.swift @@ -325,7 +325,9 @@ final class WindowManager { onWorkspaceEvent?(workspace.layout == .tiled ? String(localized: "\(columns) columns") : String(localized: "\(columns) columns when tiled")) return } - guard workspace.perform(action) else { + let changed = workspace.perform(action) + if let window = workspace.stoppedByMinimum, let minimum = workspace.minimumWidths[window] { showMinimumNotice(window, minimum: minimum) } + guard changed else { log.debug("\(action.rawValue, privacy: .public): no change. \(self.stripDescription, privacy: .public)") return } @@ -1563,12 +1565,17 @@ final class WindowManager { private func reassert() { var moved = false var resized = false + var minimumNotice: (window: WindowID, minimum: CGFloat)? defer { userResized = [] } for display in displays.indices { for id in displays[display].spaces.active.strip.windows { guard let window = windows[id], !fullScreenWindows.contains(id), let actual = window.element.frame else { continue } if userResized.contains(id), !hiddenWindows.contains(id), let applied = lastApplied[id], abs(actual.width - applied.width) > 2, let (column, _) = displays[display].spaces.active.strip.location(of: id) { + // The app held its width at its minimum, so the drag stopped there. + if let limit = displays[display].spaces.active.minimumLimit(of: id), abs(actual.width - limit.width) <= 2, applied.width > actual.width + 2 { + minimumNotice = (limit.window, limit.width) + } displays[display].spaces.active.setColumnWidthFromResize(id, to: actual.width) for sibling in displays[display].spaces.active.strip.columns[column].windows { lastApplied[sibling] = nil } displays[display].spaces.active.ensureFocusedVisible() @@ -1590,11 +1597,18 @@ final class WindowManager { if resized { log.debug("adopted a resized width: \(self.stripDescription, privacy: .public)") } + if let notice = minimumNotice { showMinimumNotice(notice.window, minimum: notice.minimum) } if moved { applyLayout() } refreshOverlays() if resized { didChange() } } + /// Says on screen why a width change stopped at an app's minimum width. + private func showMinimumNotice(_ window: WindowID, minimum: CGFloat) { + guard let pid = windows[window]?.pid, let name = NSRunningApplication(processIdentifier: pid)?.localizedName else { return } + onWorkspaceEvent?(String(localized: "\(name) has a minimum width of \(Int(minimum.rounded())) px, so Glide can't make it narrower.")) + } + private static func roughlyEqual(_ a: CGRect, _ b: CGRect) -> Bool { abs(a.minX - b.minX) <= 2 && abs(a.minY - b.minY) <= 2 && abs(a.width - b.width) <= 2 && abs(a.height - b.height) <= 2 } diff --git a/Sources/GlideCore/Workspace.swift b/Sources/GlideCore/Workspace.swift index 13882b5..1d1e2f3 100644 --- a/Sources/GlideCore/Workspace.swift +++ b/Sources/GlideCore/Workspace.swift @@ -95,6 +95,8 @@ public struct Workspace: Equatable, Sendable { public var tiledRatios: [CGFloat]? /// The window whose tile fills the screen (tiled "full width"); only counts while that tile has focus. public private(set) var zoomedWindow: WindowID? + /// The window whose learned minimum width held back the last width change, so the caller can say why; nil otherwise. + public private(set) var stoppedByMinimum: WindowID? public init(strip: Strip = Strip(), viewport: CGRect, settings: LayoutSettings = .standard) { self.strip = strip @@ -150,6 +152,21 @@ public struct Workspace: Equatable, Sendable { column.windows.compactMap { minimumWidths[$0] }.max() ?? 0 } + /// The learned minimum width of the column or tile holding `window`, and the window that sets it; nil if none is known. + public func minimumLimit(of window: WindowID) -> (window: WindowID, width: CGFloat)? { + if layout == .tiled { + let tiles = tiles() + guard let index = tiles.firstIndex(where: { tileWindows($0).contains(window) }) else { return nil } + return minimumLimit(among: tileWindows(tiles[index])) + } + guard let (column, _) = strip.location(of: window) else { return nil } + return minimumLimit(among: strip.columns[column].windows) + } + + private func minimumLimit(among windows: [WindowID]) -> (window: WindowID, width: CGFloat)? { + windows.compactMap { id in minimumWidths[id].map { (window: id, width: $0) } }.max { $0.width < $1.width } + } + /// Every column's width on the strip, worked out once. private func widths() -> [CGFloat] { strip.columns.indices.map(width(ofColumn:)) } @@ -292,6 +309,7 @@ public struct Workspace: Equatable, Sendable { /// Applies a layout action. Returns false if it changed nothing or is not a layout action. @discardableResult public mutating func perform(_ action: Action) -> Bool { + stoppedByMinimum = nil if action == .cycleTiledColumns { cycleTiledColumns() return true @@ -343,14 +361,22 @@ public struct Workspace: Equatable, Sendable { /// otherwise it steps through the presets. private mutating func cycleWidth(forward: Bool) -> Bool { guard let column = strip.focusedColumn else { return false } - guard let custom = column.customWidth, !column.isFullWidth else { - return strip.cycleWidth(presetCount: settings.widthPresets.count, forward: forward) + let changed: Bool + if let custom = column.customWidth, !column.isFullWidth { + let widths = settings.widthPresets.map(presetWidth) + let index = forward + ? widths.firstIndex { $0 > custom + 1 } ?? 0 + : widths.lastIndex { $0 < custom - 1 } ?? widths.count - 1 + changed = strip.setWidthIndex(index) + } else { + changed = strip.cycleWidth(presetCount: settings.widthPresets.count, forward: forward) + } + // A step narrower than the window's minimum still shows the minimum; say so. + if !forward, let window = strip.focusedWindow, let limit = minimumLimit(of: window), let focused = strip.focusedColumn, + presetWidth(settings.widthPresets[focused.widthIndex]) < limit.width - 0.5 { + stoppedByMinimum = limit.window } - let widths = settings.widthPresets.map(presetWidth) - let index = forward - ? widths.firstIndex { $0 > custom + 1 } ?? 0 - : widths.lastIndex { $0 < custom - 1 } ?? widths.count - 1 - return strip.setWidthIndex(index) + return changed } /// Adopts the width the user just dragged `window` to. On the strip that is its column's width. @@ -359,25 +385,27 @@ public struct Workspace: Equatable, Sendable { /// nothing changed. @discardableResult public mutating func setColumnWidthFromResize(_ window: WindowID, to width: CGFloat) -> Bool { - guard layout == .tiled else { return strip.setCustomWidth(width, forColumnOf: window) } + stoppedByMinimum = nil + guard layout == .tiled else { + var width = width + if let limit = minimumLimit(of: window), width < limit.width - 0.5 { + stoppedByMinimum = limit.window + width = limit.width + } + return strip.setCustomWidth(width, forColumnOf: window) + } let tiles = tiles() guard let index = tiles.firstIndex(where: { tileWindows($0).contains(window) }), zoomedWindow == nil else { return false } let count = tiles.count - let splits = TiledLayout.splits(in: viewport, count: count, columns: tiledColumns, gap: settings.gap, ratios: effectiveRatios(count)) - // The split that sizes the tile across: its own if that one is side by side (the tile is its first part), else the nearest earlier one (it is in the second part). - let split: Int - let isFirstPart: Bool - if index < count - 1, splits[index].sideBySide { - (split, isFirstPart) = (index, true) - } else if let earlier = (0.. 0 else { return false } - let share = min(max(width / available, 0.1), 0.9) - return setTiledRatio(split: split, count: count, to: isFirstPart ? share : 1 - share) + let share = min(max(width / sizing.available, 0.1), 0.9) + return setTiledRatio(split: sizing.split, count: count, to: sizing.isFirstPart ? share : 1 - share) } // MARK: Tiled @@ -406,20 +434,66 @@ public struct Workspace: Equatable, Sendable { return ratios } + /// The narrowest a tile without a minimum of its own is squeezed to by its neighbours' minimums (or + /// the tile's own width, if it was already narrower). + private static let neighbourFloor: CGFloat = 200 + + /// The ratios the layout is drawn with: the stored ones, with the split of each tile that has a + /// learned minimum width set to give it that width. Unset splits stay 0, so they keep their default. + private func displayedRatios(_ tiles: [Tile]) -> [CGFloat] { + fittedRatios(tiles) ?? effectiveRatios(tiles.count) ?? Array(repeating: 0, count: max(tiles.count - 1, 0)) + } + + /// The ratios that give each tile its learned minimum width by the split that sizes it, leaving the + /// other splits as they are. nil when the minimums can't all fit, or a tile would end up narrower than + /// its minimum or than `neighbourFloor`, so the caller keeps the layout as it was. + private func fittedRatios(_ tiles: [Tile]) -> [CGFloat]? { + let count = tiles.count + let original = effectiveRatios(count) ?? Array(repeating: 0, count: max(count - 1, 0)) + let minimums = tiles.map { min(minimumLimit(among: tileWindows($0))?.width ?? 0, usableWidth).rounded(.up) } + func frames(_ ratios: [CGFloat]) -> [CGRect] { + TiledLayout.frames(in: viewport, count: count, columns: tiledColumns, gap: settings.gap, ratios: ratios) + } + var ratios = original + for index in tiles.indices where minimums[index] > 0 && frames(ratios)[index].width < minimums[index] { + guard let sizing = sizing(ofTile: index, count: count, ratios: ratios), minimums[index] < sizing.available else { return nil } + let share = minimums[index] / sizing.available + ratios[sizing.split] = sizing.isFirstPart ? share : 1 - share + } + let before = frames(original), after = frames(ratios) + let fits = tiles.indices.allSatisfy { index in + after[index].width >= max(minimums[index], min(before[index].width, Self.neighbourFloor)) - 0.5 + } + return fits ? ratios : nil + } + + /// The split that sizes tile `index` across, whether the tile is its first part, and the room that + /// split divides (its width, less the gap). The tile's own split if that is side by side, else the + /// nearest earlier side-by-side split, which it sits in the second part of. + private func sizing(ofTile index: Int, count: Int, ratios: [CGFloat]) -> (split: Int, isFirstPart: Bool, available: CGFloat)? { + let splits = TiledLayout.splits(in: viewport, count: count, columns: tiledColumns, gap: settings.gap, ratios: ratios) + if index < count - 1, splits[index].sideBySide { + return (index, true, splits[index].room.width - settings.gap) + } + guard let earlier = (0.. Int? { guard let zoomed = zoomedWindow, let focused = strip.focusedColumn, focused.windows.contains(zoomed) else { return nil } return tiles.firstIndex { $0.column == strip.focusedColumnIndex && ($0.row == nil || focused.windows[$0.row!] == zoomed) } } - /// Every tile's frame as the spiral lays it out, before minimum widths and zoom: what focus - /// and move go by, and what the ratios shape. + /// Every tile's frame as the spiral lays it out, with the ratios fitted to minimum widths but before + /// zoom: what focus and move go by, and what the ratios shape. private func tileFrames(_ tiles: [Tile]) -> [CGRect] { - TiledLayout.frames(in: viewport, count: tiles.count, columns: tiledColumns, gap: settings.gap, ratios: effectiveRatios(tiles.count)) + TiledLayout.frames(in: viewport, count: tiles.count, columns: tiledColumns, gap: settings.gap, ratios: displayedRatios(tiles)) } - /// The frames tiles are sent to: a tile narrower than a window in it allows gets that window's - /// minimum width within its own x, overlapping its neighbour if it must; a zoomed tile fills the screen. + /// The frames tiles are sent to. A tile narrower than a window in it allows gets that window's minimum + /// width within its own x, overlapping its neighbour, but only when the minimums can't all fit (see + /// `fittedRatios`); a zoomed tile fills the screen. private func placedFrames(_ tiles: [Tile]) -> [CGRect] { var frames = tileFrames(tiles) let gap = settings.gap @@ -506,13 +580,26 @@ public struct Workspace: Equatable, Sendable { guard count >= 2 else { return false } let index = strip.focusedColumnIndex let split = min(index, count - 2) - let current = TiledLayout.ratio(split: split, count: count, columns: tiledColumns, ratios: effectiveRatios(count)) + let ratios = displayedRatios(tiles()) + let current = TiledLayout.ratio(split: split, count: count, columns: tiledColumns, ratios: ratios) let isLast = index == count - 1 let share = isLast ? 1 - current : current let presets = TiledLayout.ratioPresets let tolerance: CGFloat = 0.01 let next = forward ? presets.first { $0 > share + tolerance } ?? presets[0] : presets.last { $0 < share - tolerance } ?? presets[presets.count - 1] + var stepped = ratios + stepped[split] = isLast ? 1 - next : next + // A step that would leave the tile narrower than its window's minimum stops at that minimum. + if let window = strip.focusedWindow, let limit = minimumLimit(of: window), + TiledLayout.frames(in: viewport, count: count, columns: tiledColumns, gap: settings.gap, ratios: stepped)[index].width < limit.width - 0.5 { + stoppedByMinimum = limit.window + guard let sizing = sizing(ofTile: index, count: count, ratios: ratios), limit.width.rounded(.up) < sizing.available else { return false } + let minimumShare = limit.width.rounded(.up) / sizing.available + let target = sizing.isFirstPart ? minimumShare : 1 - minimumShare + guard abs(target - ratios[sizing.split]) >= 0.001 else { return false } + return setTiledRatio(split: sizing.split, count: count, to: target) + } return setTiledRatio(split: split, count: count, to: isLast ? 1 - next : next) } diff --git a/Tests/GlideCoreTests/TiledLayoutTests.swift b/Tests/GlideCoreTests/TiledLayoutTests.swift index f45cc9a..1326f70 100644 --- a/Tests/GlideCoreTests/TiledLayoutTests.swift +++ b/Tests/GlideCoreTests/TiledLayoutTests.swift @@ -91,17 +91,51 @@ final class TiledLayoutTests: XCTestCase { XCTAssertEqual(set.workspaces[1].tiledColumns, 3) } - func testMinimumWidthsGiveTheTileItsMinimumWithinItsX() { + func testAMinimumWidthShrinksTheNeighbourInsteadOfOverlapping() { + var ws = make(2) + ws.minimumWidths[1] = 800 + XCTAssertEqual(frames(ws)[1], CGRect(x: 12, y: 52, width: 800, height: 776)) + XCTAssertEqual(frames(ws)[2], CGRect(x: 824, y: 52, width: 376, height: 776), "Shrinks to sit beside it") + XCTAssertNil(ws.tiledRatios, "The stored ratios are left alone") + } + + func testMinimumWidthsThatCannotFitFallBackToOverlap() { var ws = make(2) ws.minimumWidths[1] = 800 - XCTAssertEqual(frames(ws)[1], CGRect(x: 12, y: 52, width: 800, height: 776), "Overlaps its neighbour at x = 612") - XCTAssertEqual(frames(ws)[2], CGRect(x: 612, y: 52, width: 588, height: 776)) ws.minimumWidths[2] = 700 - XCTAssertEqual(frames(ws)[2], CGRect(x: 500, y: 52, width: 700, height: 776), "Pulled left so it stays on screen") + XCTAssertEqual(frames(ws)[1], CGRect(x: 12, y: 52, width: 800, height: 776)) + XCTAssertEqual(frames(ws)[2], CGRect(x: 500, y: 52, width: 700, height: 776), "Both can't fit side by side, so the second overlaps") ws.minimumWidths[2] = 5000 XCTAssertEqual(frames(ws)[2]?.width, ws.usableWidth) } + func testANeighbourIsNotSqueezedBelowASensibleSize() { + var ws = make(2) + ws.minimumWidths[1] = 1000 + XCTAssertEqual(frames(ws)[2], CGRect(x: 612, y: 52, width: 588, height: 776), "Leaving 176 pt for the neighbour is too little, so it overlaps") + } + + func testAMinimumForASecondPartTileIsMetThroughTheEarlierSplit() { + var ws = make(3) + ws.minimumWidths[2] = 900 + XCTAssertEqual(frames(ws)[1], CGRect(x: 12, y: 52, width: 276, height: 776), "The first column gives up the room") + XCTAssertEqual(frames(ws)[2], CGRect(x: 300, y: 52, width: 900, height: 382)) + XCTAssertEqual(frames(ws)[3], CGRect(x: 300, y: 446, width: 900, height: 382)) + XCTAssertNil(ws.tiledRatios) + } + + func testAResizeOrWidthCycleBelowTheMinimumStopsAtIt() { + var ws = make(2) + ws.minimumWidths[1] = 800 + XCTAssertTrue(ws.setColumnWidthFromResize(1, to: 300)) + XCTAssertEqual(frames(ws)[1]?.width, 800) + XCTAssertEqual(ws.stoppedByMinimum, 1) + XCTAssertFalse(ws.perform(.cycleColumnWidthBackward), "Already at its minimum") + XCTAssertEqual(ws.stoppedByMinimum, 1) + XCTAssertFalse(ws.perform(.focusColumnLeft), "No tile to the left") + XCTAssertNil(ws.stoppedByMinimum) + } + func testATabbedColumnIsOneTileWithItsBarAtTheTop() { let ws = workspace([Column(windows: [1], widthIndex: 1), Column(windows: [2, 3, 4], focusedRow: 1, widthIndex: 1, isTabbed: true)]) let tile = expected(ws, count: 2)[1]