コード整理。コメント英語化

This commit is contained in:
Kirurobo
2020-11-30 10:52:40 +09:00
parent e6c18228ad
commit 7b875cf3ba
3 changed files with 114 additions and 92 deletions

View File

@@ -22,6 +22,8 @@ import Cocoa
@objcMembers
public class LibUniWinC : NSObject {
// MARK: - Internal structs and classes
//
private struct State {
public var isReady: Bool = false
@@ -119,7 +121,7 @@ public class LibUniWinC : NSObject {
private static var monitorIndices: [Int] = []
// MARK: - Methods
// MARK: - Properties
///
/// - Returns: true
@@ -149,122 +151,113 @@ public class LibUniWinC : NSObject {
return (targetWindow?.isZoomed ?? false)
}
}
@objc public static func detachWindow() -> Void {
//
if (targetWindow != nil) {
let center = NotificationCenter.default
center.removeObserver(self)
//
orgWindowInfo.Restore(window: targetWindow!)
// Remove the subview
if (overlayView != nil) {
targetWindow?.contentView?.willRemoveSubview(overlayView!)
overlayView = nil
}
targetWindow = nil
}
}
///
@objc public static func attachMyWindow() -> Bool {
//
let window: NSWindow = _findMyWindow()
_attachWindow(window: window)
return true
}
///
private static func _reapplyWindowStyles() -> Void {
if (targetWindow != nil) {
setTopmost(isTopmost: state.isTopmost)
setBorderless(isBorderless: state.isBorderless)
setTransparent(isTransparent: state.isTransparent)
}
}
// MARK: - Initialize, window handling
private static func _updateScreenSize() -> Void {
// Reference: https://stackoverrun.com/ja/q/1746184
primaryMonitorHeight = NSScreen.screens.map {$0.frame.origin.y + $0.frame.height}.max()!
//
_updateMonitorRectangles()
}
private static func _updateMonitorRectangles() -> Void {
//
monitorCount = NSScreen.screens.count
//
monitorRectangles.removeAll()
monitorIndices.removeAll()
//
for i in 0..<monitorCount {
let screen = NSScreen.screens[i]
monitorRectangles.append(screen.visibleFrame)
monitorIndices.append(i)
}
// 0
monitorIndices = monitorIndices.sorted(by: {
(monitorRectangles[$0].minX < monitorRectangles[$1].minX)
|| (monitorRectangles[$0].minX == monitorRectangles[$1].minX && monitorRectangles[$0].maxY < monitorRectangles[$1].maxY)
})
}
///
/// Initialize
private static func _setup() -> Void {
//
_updateScreenSize()
// Get the screen size
_updateScreenInfo()
//
// Prepare notification to refresh the screen size
NotificationCenter.default.addObserver(
forName: NSApplication.didChangeScreenParametersNotification,
object: NSApplication.shared,
queue: OperationQueue.main
) {
notification -> Void in
_updateScreenSize()
_updateScreenInfo()
}
// Flag as initialized
state.isReady = true
}
///
/// Retrieve current monitor settings
private static func _updateScreenInfo() -> Void {
// Reference: https://stackoverrun.com/ja/q/1746184
primaryMonitorHeight = NSScreen.screens.map {$0.frame.origin.y + $0.frame.height}.max()!
// Get the number of monitors
monitorCount = NSScreen.screens.count
// Clear the list
monitorRectangles.removeAll()
monitorIndices.removeAll()
// Get each screen rectangle
for i in 0..<monitorCount {
let screen = NSScreen.screens[i]
monitorRectangles.append(screen.visibleFrame)
monitorIndices.append(i)
}
// Sort the list so that the top left monitor is at the zero
monitorIndices = monitorIndices.sorted(by: {
(monitorRectangles[$0].minX < monitorRectangles[$1].minX)
|| (monitorRectangles[$0].minX == monitorRectangles[$1].minX && monitorRectangles[$0].maxY < monitorRectangles[$1].maxY)
})
}
/// Find my own window
private static func _findMyWindow() -> NSWindow {
let myWindow: NSWindow = NSApp.orderedWindows[0]
return myWindow
}
///
/// Detach from the window
@objc public static func detachWindow() -> Void {
if (targetWindow != nil) {
let center = NotificationCenter.default
center.removeObserver(self)
// Restore the original style
orgWindowInfo.Restore(window: targetWindow!)
// Remove the subview
if (overlayView != nil) {
overlayView?.removeFromSuperview()
overlayView = nil
}
targetWindow = nil
}
}
/// Attach to my main window
@objc public static func attachMyWindow() -> Bool {
let window: NSWindow = _findMyWindow()
_attachWindow(window: window)
return true
}
/// Set the target window
/// Restore the former winodw if exist
private static func _attachWindow(window: NSWindow) -> Void {
//
// Do nothing if the same window is the target
if (targetWindow == window) {
return
}
//
// Release the former window if exist
detachWindow()
//
// Initialize when the first call
if (!state.isReady) {
_setup()
}
//
// Set to the target
targetWindow = window
//
// Store the original state
orgWindowInfo.Store(window: window)
//
// Apply the state
_reapplyWindowStyles()
//
// Reapply the state at fullscreen
NotificationCenter.default.addObserver(
forName: NSWindow.didEnterFullScreenNotification,
object: nil,
@@ -273,7 +266,7 @@ public class LibUniWinC : NSObject {
notification -> Void in _reapplyWindowStyles()
}
//
// Reapply the state at the end of fullscreen
NotificationCenter.default.addObserver(
forName: NSWindow.didExitFullScreenNotification,
object: nil,
@@ -281,12 +274,31 @@ public class LibUniWinC : NSObject {
{
notification -> Void in _reapplyWindowStyles()
}
}
/// Create an overlay view to handle file dropping
private static func _setupOverlayView() -> Void {
guard let window = targetWindow
else {
return
}
// Add a subview to handle file dropping
overlayView = OverlayView(frame: window.frame)
window.contentView?.addSubview(overlayView!)
overlayView?.fitToSuperView()
}
/// Apply current window state
private static func _reapplyWindowStyles() -> Void {
if (targetWindow != nil) {
setTopmost(isTopmost: state.isTopmost)
setBorderless(isBorderless: state.isBorderless)
setTransparent(isTransparent: state.isTransparent)
}
}
// MARK: - Functions to get or set the window state
///
/// - Parameters:
@@ -601,6 +613,10 @@ public class LibUniWinC : NSObject {
// MARK: - File drop
@objc public static func setAllowDrop(enabled: Bool) -> Bool {
if (overlayView == nil) {
_setupOverlayView()
}
overlayView?.setEnabled(enabled: enabled)
return true
}

View File

@@ -17,7 +17,18 @@ class OverlayView: NSView {
// https://qiita.com/ohbashunsuke/items/8b9d6dc07408091690c6
// https://stackoverflow.com/questions/31657523/get-file-path-using-drag-and-drop-swift-macos
/// Temporaly enable or disable file dropping
var enabled = false
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
override init(frame: NSRect) {
super.init(frame: NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
setup()
}
public func setEnabled(enabled: Bool) {
self.enabled = enabled
@@ -27,8 +38,11 @@ class OverlayView: NSView {
self.registerForDraggedTypes(
[NSPasteboard.PasteboardType.URL, NSPasteboard.PasteboardType.fileURL]
)
self.wantsLayer = false
}
/// Set constraints to fit the window
public func fitToSuperView() -> Void {
guard let parent = superview
else {
@@ -46,16 +60,7 @@ class OverlayView: NSView {
parent.addConstraints(constraints)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
override init(frame: NSRect) {
super.init(frame: NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
setup()
}
/// Set the visual when dragging
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if (enabled) {
return .link
@@ -64,6 +69,7 @@ class OverlayView: NSView {
}
}
/// Get the paths and perform the callback
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
if (!self.enabled) {
return false
@@ -102,7 +108,7 @@ class OverlayView: NSView {
override func draw(_ dirtyRect: NSRect) {
//super.draw(dirtyRect)
// for debugging
// for debugging
// // Drawing code here.
// NSColor.red.set()
// let figure = NSBezierPath()