Skip to content

Commit

Permalink
Improve window layouts, event handling and UserLexicon
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Sep 30, 2024
1 parent 203ad18 commit 6e6c0a0
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 121 deletions.
7 changes: 3 additions & 4 deletions TypeDuck/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ final class AppContext: ObservableObject {
highlightedIndex = newHighlightedIndex
}

func updateInputForm(to form: InputForm? = nil) {
let newForm: InputForm = form ?? InputForm.matchInputMethodMode()
if newForm.isOptions {
func updateInputForm(to form: InputForm) {
if form.isOptions {
optionsHighlightedIndex = minIndex
}
inputForm = newForm
inputForm = form
}
func updateWindowPattern(to pattern: WindowPattern) {
windowPattern = pattern
Expand Down
1 change: 0 additions & 1 deletion TypeDuck/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import CoreIME

@MainActor
@main
@objc(AppDelegate)
final class AppDelegate: NSObject, NSApplicationDelegate {

static let shared: AppDelegate = AppDelegate()
Expand Down
2 changes: 1 addition & 1 deletion TypeDuck/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3142,7 +3142,7 @@
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Directly Toggle Specific Option"
"value" : "Directly Toggle a Specific Option"
}
},
"yue" : {
Expand Down
4 changes: 3 additions & 1 deletion TypeDuck/MotherBoard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct MotherBoard: View {
@EnvironmentObject private var context: AppContext

var body: some View {
ZStack(alignment: context.windowPattern.windowAlignment) {
let windowPattern = context.windowPattern
ZStack(alignment: windowPattern.windowAlignment) {
Color.clear
if context.inputForm.isOptions {
OptionsView()
Expand All @@ -15,5 +16,6 @@ struct MotherBoard: View {
CandidateBoard()
}
}
.fixedSize(horizontal: !(windowPattern.isReversingHorizontal), vertical: !(windowPattern.isReversingVertical))
}
}
1 change: 0 additions & 1 deletion TypeDuck/PrincipalApplication.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import AppKit

@MainActor
@objc(PrincipalApplication)
final class PrincipalApplication: NSApplication {

override init() {
Expand Down
171 changes: 67 additions & 104 deletions TypeDuck/TypeDuckInputController.swift

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions TypeDuck/UserLexicon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ import Foundation
import SQLite3
import CoreIME

@MainActor
struct UserLexicon: Sendable {

nonisolated(unsafe) private static var database: OpaquePointer? = nil
private static let database: OpaquePointer? = {
var db: OpaquePointer? = nil
let path: String? = {
let fileName: String = "userlexicon.sqlite3"
if #available(macOS 13.0, *) {
return URL.libraryDirectory.appending(path: fileName, directoryHint: .notDirectory).path()
} else {
guard let libraryDirectoryUrl: URL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first else { return nil }
return libraryDirectoryUrl.appendingPathComponent(fileName, isDirectory: false).path
}
}()
guard let path else { return nil }
guard sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK else { return nil }
return db
}()

static func prepare() {
guard database == nil else { return }
guard let libraryDirectoryUrl: URL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first else { return }
let userLexiconUrl: URL = libraryDirectoryUrl.appendingPathComponent("userlexicon.sqlite3", isDirectory: false)
if sqlite3_open_v2(userLexiconUrl.path, &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) == SQLITE_OK {
ensureTable()
}
}
private static func ensureTable() {
let command: String = "CREATE TABLE IF NOT EXISTS userlexicontable(id INTEGER NOT NULL PRIMARY KEY, frequency INTEGER NOT NULL, word TEXT NOT NULL, romanization TEXT NOT NULL, shortcut INTEGER NOT NULL, ping INTEGER NOT NULL);"
var statement: OpaquePointer? = nil
defer { sqlite3_finalize(statement) }
Expand Down

0 comments on commit 6e6c0a0

Please sign in to comment.