-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add macOS 10.14 Dark Mode support #287
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,8 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc | |
|
||
@IBOutlet weak var editViewHeight: NSLayoutConstraint! | ||
@IBOutlet weak var editViewWidth: NSLayoutConstraint! | ||
|
||
let appDelegate = NSApp.delegate as! AppDelegate | ||
|
||
lazy var findViewController: FindViewController! = { | ||
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) | ||
|
@@ -148,11 +150,15 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc | |
|
||
statusBar.updateStatusBarColor(newBackgroundColor: self.theme.background, newTextColor: self.theme.foreground, newUnifiedTitlebar: unifiedTitlebar) | ||
findViewController.updateColor(newBackgroundColor: self.theme.background, unifiedTitlebar: unifiedTitlebar) | ||
|
||
if color.isDark && unifiedTitlebar { | ||
window.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark) | ||
if #available(OSX 10.14, *) { | ||
// Let system decide, disregarding active theme color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
} else { | ||
window.appearance = NSAppearance(named: NSAppearance.Name.aqua) | ||
if color.isDark && unifiedTitlebar { | ||
window.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark) | ||
} else { | ||
window.appearance = NSAppearance(named: NSAppearance.Name.aqua) | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -614,6 +620,11 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc | |
// MARK: - Debug Methods | ||
@IBAction func debugSetTheme(_ sender: NSMenuItem) { | ||
guard sender.state != NSControl.StateValue.on else { print("theme already active"); return } | ||
if sender.title == "macOS" { | ||
let theme = Theme.systemTheme() | ||
appDelegate.themeChanged(name: sender.title, theme: theme) | ||
return | ||
} | ||
let req = Events.SetTheme(themeName: sender.title) | ||
document.dispatcher.coreConnection.sendRpcAsync(req.method, params: req.params!) | ||
} | ||
|
@@ -755,6 +766,12 @@ class EditViewController: NSViewController, EditViewDataSource, FindDelegate, Sc | |
.first?.title | ||
|
||
pluginsMenu.removeAllItems() | ||
|
||
let systemThemeMenuItem = NSMenuItem(title: "macOS", action: #selector(EditViewController.debugSetTheme(_:)), keyEquivalent: "") | ||
systemThemeMenuItem.state = NSControl.StateValue(rawValue: ("macOS" == currentlyActive) ? 1 : 0) | ||
|
||
pluginsMenu.addItem(systemThemeMenuItem) | ||
pluginsMenu.addItem(NSMenuItem.separator()) | ||
for theme in themes { | ||
let item = NSMenuItem(title: theme, action: #selector(EditViewController.debugSetTheme(_:)), keyEquivalent: "") | ||
item.state = NSControl.StateValue(rawValue: (theme == currentlyActive) ? 1 : 0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the right way to access AppDelegate. Not even sure if I'm supposed to access the AppDelegate in a VC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid storing the variable, and instead just call
(NSApp.delegate as? AppDelegate)?.whatever()
in line, as needed.(edit: I mean, this is still kind of code smell, but we do it. 😕)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback on this, always interesting to see various practices!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's generally not good practice to reference the AppDelegate inside of your view controllers (directly).
How about using a protocol?
e.g.
It will make it much easier to write a unit test and decouples the editor view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding to what @ollieatkinson wrote, the best option is to use dependency injection to add the delegate into the view controller:
Here, the main view controller is setting its
themeChangeDelegate
on theThemeViewController
, so it is just copying its dependency into theThemeViewController
Here, the AppDelegate sets up the
MainViewController
and installs itself as the dependency. That way, nobody ever accesses the AppDelegate except for the appdelegate itself.