Merge pull request #443 from mitchellh/hide-decorations-macos

macos: allow to hide decorations
This commit is contained in:
Mitchell Hashimoto
2023-09-17 12:07:13 -07:00
committed by GitHub
4 changed files with 102 additions and 73 deletions

View File

@ -19,7 +19,7 @@ class PrimaryWindow: NSWindow {
static func create(ghostty: Ghostty.AppState, appDelegate: AppDelegate, baseConfig: ghostty_surface_config_s? = nil) -> PrimaryWindow {
let window = PrimaryWindow(
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
styleMask: getStyleMask(renderDecoration: ghostty.windowDecorations),
backing: .buffered,
defer: false)
window.center()
@ -45,6 +45,15 @@ class PrimaryWindow: NSWindow {
return window
}
static func getStyleMask(renderDecoration: Bool) -> NSWindow.StyleMask {
var mask: NSWindow.StyleMask = [.resizable, .closable, .miniaturizable]
if renderDecoration {
mask.insert(.titled)
}
return mask
}
override var canBecomeKey: Bool {
return true
}

View File

@ -63,6 +63,15 @@ extension Ghostty {
return Info(mode: raw.build_mode, version: String(version))
}
/// True if we want to render window decorations
var windowDecorations: Bool {
guard let config = self.config else { return true }
var v = false;
let key = "window-decoration"
_ = ghostty_config_get(config, &v, key, UInt(key.count))
return v;
}
/// Cached clipboard string for `read_clipboard` callback.
private var cached_clipboard_string: String? = nil
@ -296,7 +305,8 @@ extension Ghostty {
// We only support the standard clipboard
if (location != GHOSTTY_CLIPBOARD_STANDARD) { return nil }
guard let appState = self.appState(fromSurface: userdata) else { return nil }
guard let surface = self.surfaceUserdata(from: userdata) else { return nil }
guard let appState = self.appState(fromView: surface) else { return nil }
guard let str = NSPasteboard.general.string(forType: .string) else { return nil }
// Ghostty requires we cache the string because the pointer we return has to remain
@ -376,6 +386,17 @@ extension Ghostty {
static func newTab(_ userdata: UnsafeMutableRawPointer?, config: ghostty_surface_config_s) {
guard let surface = self.surfaceUserdata(from: userdata) else { return }
guard let appState = self.appState(fromView: surface) else { return }
guard appState.windowDecorations else {
let alert = NSAlert()
alert.messageText = "Tabs are disabled"
alert.informativeText = "Enable window decorations to use tabs"
alert.addButton(withTitle: "OK")
alert.alertStyle = .warning
_ = alert.runModal()
return
}
NotificationCenter.default.post(
name: Notification.ghosttyNewTab,
object: surface,
@ -398,9 +419,8 @@ extension Ghostty {
}
/// Returns the GhosttyState from the given userdata value.
static private func appState(fromSurface userdata: UnsafeMutableRawPointer?) -> AppState? {
let surfaceView = Unmanaged<SurfaceView>.fromOpaque(userdata!).takeUnretainedValue()
guard let surface = surfaceView.surface else { return nil }
static private func appState(fromView view: SurfaceView) -> AppState? {
guard let surface = view.surface else { return nil }
guard let app = ghostty_surface_app(surface) else { return nil }
guard let app_ud = ghostty_app_userdata(app) else { return nil }
return Unmanaged<AppState>.fromOpaque(app_ud).takeUnretainedValue()

View File

@ -1,16 +1,16 @@
import SwiftUI
import GhosttyKit
class FullScreenHandler {
var previousTabGroup: NSWindowTabGroup?
class FullScreenHandler { var previousTabGroup: NSWindowTabGroup?
var previousTabGroupIndex: Int?
var previousContentFrame: NSRect?
var isInFullscreen: Bool = false
var previousStyleMask: NSWindow.StyleMask? = nil
// We keep track of whether we entered non-native fullscreen in case
// a user goes to fullscreen, changes the config to disable non-native fullscreen
// and then wants to toggle it off
var isInNonNativeFullscreen: Bool = false
var isInFullscreen: Bool = false
func toggleFullscreen(window: NSWindow, nonNativeFullscreen: ghostty_non_native_fullscreen_e) {
let useNonNativeFullscreen = nonNativeFullscreen != GHOSTTY_NON_NATIVE_FULLSCREEN_FALSE
@ -89,6 +89,7 @@ class FullScreenHandler {
// This is important: it gives us the full screen, including the
// notch area on MacBooks.
self.previousStyleMask = window.styleMask
window.styleMask.remove(.titled)
// Set frame to screen size, accounting for the menu bar if needed
@ -126,8 +127,8 @@ class FullScreenHandler {
func leaveFullscreen(window: NSWindow) {
guard let previousFrame = previousContentFrame else { return }
// Restore title bar
window.styleMask.insert(.titled)
// Restore the style mask
window.styleMask = self.previousStyleMask!
// Restore previous presentation options
NSApp.presentationOptions = []

View File

@ -242,7 +242,6 @@ keybind: Keybinds = .{},
/// If false, windows won't have native decorations, i.e. titlebar and
/// borders.
/// Currently only supported with GTK.
@"window-decoration": bool = true,
/// Whether to allow programs running in the terminal to read/write to