Skip to content

Commit

Permalink
Don't show elements for disabled sections in interface
Browse files Browse the repository at this point in the history
Fixes #196
  • Loading branch information
jordanbaird committed Jul 3, 2024
1 parent e65090b commit 8f23b98
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
37 changes: 36 additions & 1 deletion Ice/ControlItem/ControlItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ final class ControlItem: ObservableObject {
}
.store(in: &c)

statusItem.publisher(for: \.isVisible)
.receive(on: DispatchQueue.main)
.sink { [weak self] isVisible in
guard
let self,
let appState,
let section
else {
return
}

let manager = appState.settingsManager.hotkeySettingsManager

let hotkey: Hotkey? = switch section.name {
case .visible: nil
case .hidden: manager.hotkey(withAction: .toggleHiddenSection)
case .alwaysHidden: manager.hotkey(withAction: .toggleAlwaysHiddenSection)
}

guard let hotkey else {
return
}

if isVisible {
hotkey.enable()
} else {
hotkey.disable()
}
}
.store(in: &c)

if let window {
window.publisher(for: \.frame)
.sink { [weak self, weak window] frame in
Expand Down Expand Up @@ -387,7 +418,11 @@ final class ControlItem: ObservableObject {
// add menu items to toggle the hidden and always-hidden sections
let sectionNames: [MenuBarSection.Name] = [.hidden, .alwaysHidden]
for name in sectionNames {
guard let section = appState.menuBarManager.section(withName: name) else {
guard
let section = appState.menuBarManager.section(withName: name),
section.controlItem.isAddedToMenuBar
else {
// section doesn't exist, or is disabled
continue
}
let item = NSMenuItem(
Expand Down
8 changes: 8 additions & 0 deletions Ice/MenuBar/MenuBarSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ final class MenuBarSection: ObservableObject {
}
}

var isEnabled: Bool {
controlItem.isAddedToMenuBar
}

init(name: Name, controlItem: ControlItem) {
self.name = name
self.controlItem = controlItem
Expand Down Expand Up @@ -121,6 +125,10 @@ final class MenuBarSection: ObservableObject {
else {
return
}
guard controlItem.isAddedToMenuBar else {
// section is disabled
return
}
switch name {
case .visible where useIceBar, .hidden where useIceBar:
Task {
Expand Down
15 changes: 13 additions & 2 deletions Ice/Settings/SettingsPanes/HotkeysSettingsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ struct HotkeysSettingsPane: View {
var body: some View {
Form {
Section("Menu Bar Sections") {
hotkeyRecorder(forAction: .toggleHiddenSection)
hotkeyRecorder(forAction: .toggleAlwaysHiddenSection)
hotkeyRecorder(forSection: .hidden)
hotkeyRecorder(forSection: .alwaysHidden)
}
Section("Other") {
hotkeyRecorder(forAction: .toggleApplicationMenus)
Expand Down Expand Up @@ -46,4 +46,15 @@ struct HotkeysSettingsPane: View {
}
}
}

@ViewBuilder
private func hotkeyRecorder(forSection name: MenuBarSection.Name) -> some View {
if appState.menuBarManager.section(withName: name)?.isEnabled == true {
if case .hidden = name {
hotkeyRecorder(forAction: .toggleHiddenSection)
} else if case .alwaysHidden = name {
hotkeyRecorder(forAction: .toggleAlwaysHiddenSection)
}
}
}
}
15 changes: 12 additions & 3 deletions Ice/Settings/SettingsPanes/MenuBarItemsSettingsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ struct MenuBarItemsSettingsPane: View {
@ViewBuilder
private var layoutViews: some View {
Form {
if let visibleSection = appState.menuBarManager.section(withName: .visible) {
if
let visibleSection = appState.menuBarManager.section(withName: .visible),
visibleSection.isEnabled
{
Section(visibleSection.name.menuString) {
LayoutBar(section: visibleSection)
.annotation {
Expand All @@ -41,7 +44,10 @@ struct MenuBarItemsSettingsPane: View {
}
}

if let hiddenSection = appState.menuBarManager.section(withName: .hidden) {
if
let hiddenSection = appState.menuBarManager.section(withName: .hidden),
hiddenSection.isEnabled
{
Spacer()
.frame(maxHeight: 25)

Expand All @@ -53,7 +59,10 @@ struct MenuBarItemsSettingsPane: View {
}
}

if let alwaysHiddenSection = appState.menuBarManager.section(withName: .alwaysHidden) {
if
let alwaysHiddenSection = appState.menuBarManager.section(withName: .alwaysHidden),
alwaysHiddenSection.isEnabled
{
Spacer()
.frame(maxHeight: 25)

Expand Down

0 comments on commit 8f23b98

Please sign in to comment.