Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Update internal WindowOption bounds on change
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie committed Apr 28, 2022
1 parent a95bcff commit 6e9a94d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions window.go
Expand Up @@ -50,6 +50,7 @@ const (
eventNameWindowEventMessageCallback = "window.event.message.callback"
EventNameWindowEventMinimize = "window.event.minimize"
EventNameWindowEventMove = "window.event.move"
EventNameWindowEventMoved = "window.event.moved"
EventNameWindowEventReadyToShow = "window.event.ready.to.show"
EventNameWindowEventResize = "window.event.resize"
EventNameWindowEventResizeContent = "window.event.resize.content"
Expand All @@ -59,6 +60,7 @@ const (
EventNameWindowEventUnresponsive = "window.event.unresponsive"
EventNameWindowEventDidGetRedirectRequest = "window.event.did.get.redirect.request"
EventNameWindowEventWebContentsExecutedJavaScript = "window.event.web.contents.executed.javascript"
EventNameWindowEventWillMove = "window.event.will.move"
EventNameWindowEventWillNavigate = "window.event.will.navigate"
EventNameWindowEventUpdatedCustomOptions = "window.event.updated.custom.options"
EventNameWindowEventAlwaysOnTopChanged = "window.event.always.on.top.changed"
Expand Down Expand Up @@ -246,6 +248,27 @@ func newWindow(ctx context.Context, l astikit.SeverityLogger, o Options, p Paths
return
})

// Bounds change handling, updates the internal WindowOption's bounds
updateBoundsFunc := func(w *Window) func(e Event) (deleteListener bool) {
return func(e Event) (deleteListener bool) {
w.m.Lock()
defer w.m.Unlock()
w.o.X = e.Bounds.X
w.o.Y = e.Bounds.Y
w.o.Width = e.Bounds.Width
w.o.Height = e.Bounds.Height
return
}
}

w.On(EventNameWindowEventMaximize, updateBoundsFunc(w))
w.On(EventNameWindowEventMove, updateBoundsFunc(w))
w.On(EventNameWindowEventMoved, updateBoundsFunc(w))
w.On(EventNameWindowEventResize, updateBoundsFunc(w))
w.On(EventNameWindowEventResizeContent, updateBoundsFunc(w))
w.On(EventNameWindowEventUnmaximize, updateBoundsFunc(w))
w.On(EventNameWindowEventWillMove, updateBoundsFunc(w))

// Basic parse
if w.url, err = stdUrl.Parse(url); err != nil {
err = fmt.Errorf("std parsing of url %s failed: %w", url, err)
Expand All @@ -263,6 +286,7 @@ func newWindow(ctx context.Context, l astikit.SeverityLogger, o Options, p Paths
// Set url
w.url = &stdUrl.URL{Path: filepath.ToSlash(url), Scheme: "file"}
}

return
}

Expand All @@ -280,6 +304,26 @@ func (w *Window) Blur() (err error) {
return
}

// Bounds return the window bounds
func (w *Window) Bounds() (rect Rectangle, err error) {
if err = w.ctx.Err(); err != nil {
return
}
w.m.Lock()
defer w.m.Unlock()

if w.o.Width != nil && w.o.Height != nil {
rect.Size.Width = *w.o.Width
rect.Size.Height = *w.o.Height
}

if w.o.X != nil && w.o.Y != nil {
rect.Position.X = *w.o.X
rect.Position.Y = *w.o.Y
}
return
}

// Center centers the window
func (w *Window) Center() (err error) {
if err = w.ctx.Err(); err != nil {
Expand Down

0 comments on commit 6e9a94d

Please sign in to comment.