adds first version of the app
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import AppKit
|
||||
|
||||
/// What a double-click on a title bar does, after the macOS setting.
|
||||
enum TitleBarDoubleClick: Equatable {
|
||||
case zoom
|
||||
case minimize
|
||||
case none
|
||||
|
||||
/// Reads `AppleActionOnDoubleClick`; any value other than Minimize or None zooms, as the system does.
|
||||
init(setting: String?) {
|
||||
switch setting {
|
||||
case "Minimize": self = .minimize
|
||||
case "None": self = .none
|
||||
default: self = .zoom
|
||||
}
|
||||
}
|
||||
|
||||
static var current: TitleBarDoubleClick {
|
||||
TitleBarDoubleClick(setting: UserDefaults.standard.string(forKey: "AppleActionOnDoubleClick"))
|
||||
}
|
||||
}
|
||||
|
||||
/// The top of the window, over the text under the transparent title bar: it moves the window and never draws.
|
||||
final class DragStripView: NSView {
|
||||
static let height: CGFloat = 28
|
||||
weak var scrollTarget: NSScrollView?
|
||||
weak var dropTarget: NSTextView?
|
||||
|
||||
override var isOpaque: Bool { false }
|
||||
override var mouseDownCanMoveWindow: Bool { true }
|
||||
override var acceptsFirstResponder: Bool { false }
|
||||
|
||||
/// The window drags at once, also when it is not key.
|
||||
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||
|
||||
/// Leaves the window buttons to themselves.
|
||||
override func hitTest(_ point: NSPoint) -> NSView? {
|
||||
guard let view = super.hitTest(point) else { return nil }
|
||||
guard let window, let superview else { return view }
|
||||
let inWindow = superview.convert(point, to: nil)
|
||||
for type in [NSWindow.ButtonType.closeButton, .miniaturizeButton, .zoomButton] {
|
||||
guard let button = window.standardWindowButton(type), !button.isHidden, let holder = button.superview else { continue }
|
||||
if holder.convert(button.frame, to: nil).contains(inWindow) { return nil }
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// MARK: Pointer
|
||||
|
||||
/// The text view below sets the I-beam for these points too; `PageTextView` gives way, and this sets the arrow also when the window is not key.
|
||||
override func updateTrackingAreas() {
|
||||
super.updateTrackingAreas()
|
||||
guard trackingAreas.isEmpty else { return }
|
||||
addTrackingArea(NSTrackingArea(rect: .zero, options: [.cursorUpdate, .mouseEnteredAndExited, .mouseMoved, .activeAlways, .inVisibleRect], owner: self))
|
||||
}
|
||||
|
||||
override func cursorUpdate(with event: NSEvent) {
|
||||
NSCursor.arrow.set()
|
||||
}
|
||||
|
||||
override func mouseEntered(with event: NSEvent) {
|
||||
NSCursor.arrow.set()
|
||||
}
|
||||
|
||||
override func mouseMoved(with event: NSEvent) {
|
||||
NSCursor.arrow.set()
|
||||
}
|
||||
|
||||
override func mouseExited(with event: NSEvent) {}
|
||||
|
||||
// MARK: Drag
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
guard let window else { return }
|
||||
guard event.clickCount == 2 else {
|
||||
window.performDrag(with: event)
|
||||
return
|
||||
}
|
||||
switch TitleBarDoubleClick.current {
|
||||
case .zoom: window.zoom(nil)
|
||||
case .minimize: window.miniaturize(nil)
|
||||
case .none: break
|
||||
}
|
||||
}
|
||||
|
||||
override func scrollWheel(with event: NSEvent) {
|
||||
scrollTarget?.scrollWheel(with: event)
|
||||
}
|
||||
|
||||
// MARK: Drops
|
||||
|
||||
/// A view above a drop target hides it from the drag, so the strip takes the drag and hands every step on.
|
||||
func setDropTarget(_ view: NSTextView) {
|
||||
dropTarget = view
|
||||
registerForDraggedTypes(view.registeredDraggedTypes)
|
||||
}
|
||||
|
||||
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
|
||||
dropTarget?.draggingEntered(sender) ?? []
|
||||
}
|
||||
|
||||
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
|
||||
dropTarget?.draggingUpdated(sender) ?? []
|
||||
}
|
||||
|
||||
override func draggingExited(_ sender: NSDraggingInfo?) {
|
||||
dropTarget?.draggingExited(sender)
|
||||
}
|
||||
|
||||
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
|
||||
dropTarget?.prepareForDragOperation(sender) ?? false
|
||||
}
|
||||
|
||||
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
|
||||
dropTarget?.performDragOperation(sender) ?? false
|
||||
}
|
||||
|
||||
override func concludeDragOperation(_ sender: NSDraggingInfo?) {
|
||||
dropTarget?.concludeDragOperation(sender)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import AppKit
|
||||
import PlakServices
|
||||
|
||||
/// The one window: paper up to the top edge, no title, hidden instead of closed.
|
||||
public final class PageWindowController: NSWindowController, NSWindowDelegate {
|
||||
public static let frameName = "PlakPage"
|
||||
public let page: PageViewController
|
||||
/// Hides Plak on Esc, ⌘W and the close button; without it the window only orders out.
|
||||
public var onHide: (() -> Void)?
|
||||
|
||||
public init(store: PageStore, now: @escaping () -> Date = Date.init) {
|
||||
page = PageViewController(store: store, now: now)
|
||||
let window = NSWindow(
|
||||
contentRect: NSRect(x: 0, y: 0, width: 920, height: 760),
|
||||
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
|
||||
backing: .buffered,
|
||||
defer: false
|
||||
)
|
||||
window.title = "Plak"
|
||||
window.titleVisibility = .hidden
|
||||
window.titlebarAppearsTransparent = true
|
||||
window.appearance = NSAppearance(named: .aqua)
|
||||
window.backgroundColor = Theme.paper
|
||||
window.isReleasedWhenClosed = false
|
||||
window.minSize = NSSize(width: 420, height: 320)
|
||||
window.collectionBehavior = [.moveToActiveSpace, .fullScreenAuxiliary]
|
||||
window.contentViewController = page
|
||||
if !window.setFrameUsingName(Self.frameName) {
|
||||
window.setContentSize(NSSize(width: 920, height: 760))
|
||||
window.center()
|
||||
}
|
||||
window.setFrameAutosaveName(Self.frameName)
|
||||
super.init(window: window)
|
||||
window.delegate = self
|
||||
page.textView.onHide = { [weak self] in self?.hide() }
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") }
|
||||
|
||||
/// Shows the window scrolled to the end of the page: at launch and on a reopen from Spotlight or Finder.
|
||||
public func showAtEnd() {
|
||||
showWindow(nil)
|
||||
bringToFront()
|
||||
page.showEnd()
|
||||
}
|
||||
|
||||
/// Shows the window again where it was, without scrolling.
|
||||
public func reveal() {
|
||||
bringToFront()
|
||||
}
|
||||
|
||||
/// orderFrontRegardless puts the window in front even while the activation of the app still lags.
|
||||
private func bringToFront() {
|
||||
window?.makeKeyAndOrderFront(nil)
|
||||
window?.orderFrontRegardless()
|
||||
}
|
||||
|
||||
public func windowShouldClose(_ sender: NSWindow) -> Bool {
|
||||
hide()
|
||||
return false
|
||||
}
|
||||
|
||||
private func hide() {
|
||||
if let onHide {
|
||||
onHide()
|
||||
} else {
|
||||
window?.orderOut(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user