macos: iOS Ghostty.App converted to use Ghostty.Config

This commit is contained in:
Mitchell Hashimoto
2024-01-14 15:55:31 -08:00
parent eba3d5414d
commit 5e69b30240

View File

@ -12,16 +12,10 @@ extension Ghostty {
/// The readiness value of the state. /// The readiness value of the state.
@Published var readiness: Readiness = .loading @Published var readiness: Readiness = .loading
/// The ghostty global configuration. This should only be changed when it is definitely /// The global app configuration. This defines the app level configuration plus any behavior
/// safe to change. It is definitely safe to change only when the embedded app runtime /// for new windows, tabs, etc. Note that when creating a new window, it may inherit some
/// in Ghostty says so (usually, only in a reload configuration callback). /// configuration (i.e. font size) from the previously focused window. This would override this.
@Published var config: ghostty_config_t? = nil { private(set) var config: Config
didSet {
// Free the old value whenever we change
guard let old = oldValue else { return }
ghostty_config_free(old)
}
}
/// The ghostty app instance. We only have one of these for the entire app, although I guess /// The ghostty app instance. We only have one of these for the entire app, although I guess
/// in theory you can have multiple... I don't know why you would... /// in theory you can have multiple... I don't know why you would...
@ -34,18 +28,17 @@ extension Ghostty {
init() { init() {
// Initialize ghostty global state. This happens once per process. // Initialize ghostty global state. This happens once per process.
guard ghostty_init() == GHOSTTY_SUCCESS else { if ghostty_init() != GHOSTTY_SUCCESS {
logger.critical("ghostty_init failed") logger.critical("ghostty_init failed, weird things may happen")
readiness = .error readiness = .error
return
} }
// Initialize the global configuration. // Initialize the global configuration.
guard let cfg = Self.loadConfig() else { self.config = Config()
if self.config.config == nil {
readiness = .error readiness = .error
return return
} }
self.config = cfg;
// Create our "runtime" config. The "runtime" is the configuration that ghostty // Create our "runtime" config. The "runtime" is the configuration that ghostty
// uses to interface with the application runtime environment. // uses to interface with the application runtime environment.
@ -83,7 +76,7 @@ extension Ghostty {
) )
// Create the ghostty app. // Create the ghostty app.
guard let app = ghostty_app_new(&runtime_cfg, cfg) else { guard let app = ghostty_app_new(&runtime_cfg, config.config) else {
logger.critical("ghostty_app_new failed") logger.critical("ghostty_app_new failed")
readiness = .error readiness = .error
return return
@ -105,7 +98,6 @@ extension Ghostty {
deinit { deinit {
// This will force the didSet callbacks to run which free. // This will force the didSet callbacks to run which free.
self.app = nil self.app = nil
self.config = nil
#if os(macOS) #if os(macOS)
// Remove our observer // Remove our observer
@ -116,49 +108,6 @@ extension Ghostty {
#endif #endif
} }
// MARK: - Config
/// Initializes a new configuration and loads all the values.
static private func loadConfig() -> ghostty_config_t? {
// Initialize the global configuration.
guard let cfg = ghostty_config_new() else {
logger.critical("ghostty_config_new failed")
return nil
}
// Load our configuration from files, CLI args, and then any referenced files.
// We only do this on macOS because other Apple platforms do not have the
// same filesystem concept.
#if os(macOS)
ghostty_config_load_default_files(cfg);
ghostty_config_load_cli_args(cfg);
ghostty_config_load_recursive_files(cfg);
#endif
// TODO: we'd probably do some config loading here... for now we'd
// have to do this synchronously. When we support config updating we can do
// this async and update later.
// Finalize will make our defaults available.
ghostty_config_finalize(cfg)
// Log any configuration errors. These will be automatically shown in a
// pop-up window too.
let errCount = ghostty_config_errors_count(cfg)
if errCount > 0 {
logger.warning("config error: \(errCount) configuration errors on reload")
var errors: [String] = [];
for i in 0..<errCount {
let err = ghostty_config_get_error(cfg, UInt32(i))
let message = String(cString: err.message)
errors.append(message)
logger.warning("config error: \(message)")
}
}
return cfg
}
// MARK: Ghostty Callbacks // MARK: Ghostty Callbacks
static func wakeup(_ userdata: UnsafeMutableRawPointer?) {} static func wakeup(_ userdata: UnsafeMutableRawPointer?) {}