Skip to content

app: add support to hidden minimize and maximize buttons on macos and … #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type Config struct {
CustomRenderer bool
// Decorated reports whether window decorations are provided automatically.
Decorated bool

// MinimizeButtonHidden hide the window's minimize button (only works for windows and macOS)
MinimizeButtonHidden bool
// MaximizeButtonHidden hide the window's maximize button (only works for windows and macOS)
MaximizeButtonHidden bool

// Focused reports whether has the keyboard focus.
Focused bool
// decoHeight is the height of the fallback decoration for platforms such
Expand Down
15 changes: 13 additions & 2 deletions app/os_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,11 @@ func (w *window) Configure(options []Option) {
C.setWindowTitleVisibility(window, titleVis)
C.setWindowStyleMask(window, mask)
C.setWindowStandardButtonHidden(window, C.NSWindowCloseButton, barTrans)
C.setWindowStandardButtonHidden(window, C.NSWindowMiniaturizeButton, barTrans)
C.setWindowStandardButtonHidden(window, C.NSWindowZoomButton, barTrans)
// no decorated or hide minimize and maximize buttons
w.config.MinimizeButtonHidden = cnf.MinimizeButtonHidden
w.config.MaximizeButtonHidden = cnf.MaximizeButtonHidden
C.setWindowStandardButtonHidden(window, C.NSWindowMiniaturizeButton, toInt(!cnf.Decorated || cnf.MinimizeButtonHidden))
C.setWindowStandardButtonHidden(window, C.NSWindowZoomButton, toInt(!cnf.Decorated || cnf.MaximizeButtonHidden))
// When toggling the titlebar, the layer doesn't update its frame
// until the next resize. Force it.
C.resetLayerFrame(w.view)
Expand Down Expand Up @@ -1161,6 +1164,14 @@ func convertMods(mods C.NSUInteger) key.Modifiers {
return kmods
}

// toInt golang bool to C.int
func toInt(v bool) C.int {
if v {
return C.int(C.YES)
}
return C.int(C.NO)
}

func (AppKitViewEvent) implementsViewEvent() {}
func (AppKitViewEvent) ImplementsEvent() {}
func (a AppKitViewEvent) Valid() bool {
Expand Down
20 changes: 12 additions & 8 deletions app/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,16 +750,20 @@ func (w *window) Configure(options []Option) {
swpStyle |= windows.SWP_NOMOVE | windows.SWP_NOSIZE
showMode = windows.SW_SHOWMAXIMIZED
}

// Disable maximize button if MaxSize is set.
if cnf.MaxSize != (image.Point{X: 0, Y: 0}) {
if w.config.MinimizeButtonHidden {
style &^= windows.WS_MINIMIZEBOX
} else {
style |= windows.WS_MINIMIZEBOX
}
if w.config.MaximizeButtonHidden {
style &^= windows.WS_MAXIMIZEBOX
// Disable window resizing if MinSize and MaxSize are equal.
if cnf.MinSize == cnf.MaxSize {
style &^= windows.WS_THICKFRAME
}
} else {
style |= windows.WS_MAXIMIZEBOX
}
// Disable window resizing if MinSize and MaxSize are equal.
if cnf.MaxSize != (image.Point{X: 0, Y: 0}) && cnf.MinSize == cnf.MaxSize {
style &^= windows.WS_THICKFRAME
}

windows.SetWindowLong(w.hwnd, windows.GWL_STYLE, style)
windows.SetWindowPos(w.hwnd, 0, x, y, width, height, swpStyle)
windows.ShowWindow(w.hwnd, showMode)
Expand Down
18 changes: 18 additions & 0 deletions app/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,24 @@ func Decorated(enabled bool) Option {
}
}

// MinimizeButtonHidden hides the minimize button of the window.
//
// MinimizeButtonHidden is supported on Windows and macOS.
func MinimizeButtonHidden(hidden bool) Option {
return func(_ unit.Metric, cnf *Config) {
cnf.MinimizeButtonHidden = hidden
}
}

// MaximizeButtonHidden hides the maximize button of the window.
//
// MaximizeButtonHidden is supported on Windows and macOS.
func MaximizeButtonHidden(hidden bool) Option {
return func(_ unit.Metric, cnf *Config) {
cnf.MaximizeButtonHidden = hidden
}
}

// flushEvent is sent to detect when the user program
// has completed processing of all prior events. Its an
// [io/event.Event] but only for internal use.
Expand Down