Skip to content

Commit

Permalink
Toggleのチェックマークずれ問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Nov 12, 2023
1 parent 64ff4c3 commit 43395e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
45 changes: 35 additions & 10 deletions ShiftWindow/Domain/ViewModel/MenuViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ final class MenuViewModelImpl: MenuViewModel {
}
.store(in: &cancellables)

hideIcons = checkIconsVisible()
_hideIcons.projectedValue
.dropFirst()
.sink { [weak self] value in
Expand All @@ -66,9 +67,7 @@ final class MenuViewModelImpl: MenuViewModel {

NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)
.sink { [weak self] _ in
if let self, hideIcons {
toggleIconsVisible(false)
}
self?.toggleIconsVisible(false)
}
.store(in: &cancellables)
}
Expand All @@ -77,13 +76,41 @@ final class MenuViewModelImpl: MenuViewModel {
shiftModel.shiftWindow(shiftType: shiftType)
}

private func toggleIconsVisible(_ hideIcons: Bool) {
let args: String = hideIcons ? .createDesktopFalse : .createDesktopTrue
private func checkIconsVisible() -> Bool {
let shell = Process()
let pipe = Pipe()
shell.launchPath = "/bin/sh"
shell.arguments = ["-c", args]
shell.launch()
shell.waitUntilExit()
shell.arguments = ["-c", .createDesktopRead]
shell.standardOutput = pipe
do {
try shell.run()
shell.waitUntilExit()
if shell.terminationStatus == 0,
let data = try pipe.fileHandleForReading.readToEnd(),
let str = String(data: data, encoding: .utf8),
str.trimmingCharacters(in: .newlines) == "0" {
return true
}
} catch {
logput(error.localizedDescription)
}
return false
}

private func toggleIconsVisible(_ hideIcons: Bool) {
// It needs to be other than the main thread in order for reflecting Toggle check mark.
Task {
let args: String = hideIcons ? .createDesktopWriteFalse : .createDesktopDelete
let shell = Process()
shell.launchPath = "/bin/sh"
shell.arguments = ["-c", args]
do {
try shell.run()
shell.waitUntilExit()
} catch {
logput(error.localizedDescription)
}
}
}

func openSettings() {
Expand Down Expand Up @@ -111,8 +138,6 @@ extension PreviewMock {
init() {}

func shiftWindow(shiftType: ShiftType) {}
func toggleIconsVisible() {}
func resetIconsVisible() {}
func openSettings() {}
func openAbout() {}
func terminateApp() {}
Expand Down
5 changes: 3 additions & 2 deletions ShiftWindow/Helper/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation

extension String {
static let createDesktopTrue = "defaults delete com.apple.finder CreateDesktop; killall Finder"
static let createDesktopFalse = "defaults write com.apple.finder CreateDesktop -bool FALSE; killall Finder"
static let createDesktopRead = "defaults read com.apple.finder CreateDesktop"
static let createDesktopDelete = "defaults delete com.apple.finder CreateDesktop; killall Finder"
static let createDesktopWriteFalse = "defaults write com.apple.finder CreateDesktop -bool FALSE; killall Finder"
}

0 comments on commit 43395e0

Please sign in to comment.