Skip to content

Commit

Permalink
Merge pull request #23 from winebarrel/add_settings_view
Browse files Browse the repository at this point in the history
Add SettingsView
  • Loading branch information
winebarrel authored Nov 24, 2024
2 parents b956ba6 + 555917a commit c10408b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ playground.xcworkspace
# .swiftpm

.build/
build/
build.log

# CocoaPods
#
Expand Down
6 changes: 6 additions & 0 deletions Pulse/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

enum Constants {
static let defaultGithubQuery = "is:open is:pr author:@me archived:false"
static let defaultInterval: TimeInterval = 60
}
37 changes: 37 additions & 0 deletions Pulse/PreActionButtonStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// from https://zenn.dev/kyome/articles/a8ac1eb11b3c1d
import SwiftUI

struct PreActionButtonStyle: PrimitiveButtonStyle {
let preAction: () -> Void

init(preAction: @escaping () -> Void) {
self.preAction = preAction
}

func makeBody(configuration: Configuration) -> some View {
Button(role: configuration.role) {
preAction()
configuration.trigger()
} label: {
configuration.label
}
}
}

struct PreActionButtonStyleModifier: ViewModifier {
let preAction: () -> Void

init(preAction: @escaping () -> Void) {
self.preAction = preAction
}

func body(content: Content) -> some View {
content.buttonStyle(PreActionButtonStyle(preAction: preAction))
}
}

extension View {
func preActionButtonStyle(preAction: @escaping () -> Void) -> some View {
modifier(PreActionButtonStyleModifier(preAction: preAction))
}
}
7 changes: 7 additions & 0 deletions Pulse/PulseApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftUI
struct PulseApp: App {
@State private var initialized = false
@State private var isMenuPresented = false
@State private var githubToken = Vault.githubToken

private var popover: NSPopover = {
let pop = NSPopover()
Expand Down Expand Up @@ -52,5 +53,11 @@ struct PulseApp: App {
button.addSubview(mouseHandlerView)
}
}
Settings {
SettingView(githubToken: $githubToken)
.onClosed {
// TODO:

Check warning on line 59 in Pulse/PulseApp.swift

View workflow job for this annotation

GitHub Actions / Lint

Todo Violation: TODOs should be resolved (todo)
}
}
}
}
11 changes: 11 additions & 0 deletions Pulse/RightClickMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import SwiftUI

struct RightClickMenuView: View {
var body: some View {
Divider()
Button("TODO") {
// TODO:

Check warning on line 7 in Pulse/RightClickMenuView.swift

View workflow job for this annotation

GitHub Actions / Lint

Todo Violation: TODOs should be resolved (todo)
}
Divider()
SettingsLink {
Text("Settings")
}.preActionButtonStyle {
NSApp.activate(ignoringOtherApps: true)
}
Divider()
Button("Quit") {
NSApplication.shared.terminate(self)
}
Expand Down
72 changes: 72 additions & 0 deletions Pulse/SettingsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import ServiceManagement
import SwiftUI

struct SettingView: View {
@Binding var githubToken: String
@AppStorage("interval") private var interval = Constants.defaultInterval
@AppStorage("githubQuery") private var githubQuery = Constants.defaultGithubQuery
@State private var launchAtLogin: Bool = SMAppService.mainApp.status == .enabled

var body: some View {
Form {
SecureField(text: $githubToken) {
Link("GitHub token", destination: URL(string: "https://github.com/settings/tokens")!)
}.onChange(of: githubToken) {
Vault.githubToken = githubToken
}
TextField("Interval (sec)", value: $interval, format: .number.grouping(.never))
.onChange(of: interval) {
if interval < 1 {
interval = 1
} else if interval > 3600 {
interval = 3600
}
}
HStack {
TextField(text: $githubQuery, axis: .vertical) {
Link("Search query", destination: URL(string: "https://docs.github.com/search-github/searching-on-github/searching-issues-and-pull-requests")!)
}
.lineLimit(5...)
Link(destination: URL(string: "https://github.com/pulls?q=" + (githubQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""))!) {
Image(systemName: "magnifyingglass")
}
}
Toggle("Launch at login", isOn: $launchAtLogin)
.onChange(of: launchAtLogin) {
do {
if launchAtLogin {
try SMAppService.mainApp.register()
} else {
try SMAppService.mainApp.unregister()
}
} catch {
Logger.shared.error("failed to update 'Launch at login': \(error)")
}
}
Link(destination: URL(string: "https://github.com/winebarrel/Pulse")!) {
// swiftlint:disable force_cast
let appVer = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
let buildVer = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
// swiftlint:enable force_cast
Text("Ver. \(appVer).\(buildVer)")
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
.padding(20)
.frame(width: 400)
}

func onClosed(_ action: @escaping () -> Void) -> some View {
onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { notification in
if let window = notification.object as? NSWindow, window.title == "Pulse Settings" {
action()
}
}
}
}

#Preview {
SettingView(
githubToken: .constant("")
)
}

0 comments on commit c10408b

Please sign in to comment.