Skip to content

Commit

Permalink
add window.resizable property (#124)
Browse files Browse the repository at this point in the history
* add window.resizable property

* document the `resizable` property

---------

Co-authored-by: Christian Swinehart <[email protected]>
  • Loading branch information
nornagon and samizdatco authored Oct 24, 2024
1 parent 6616a6f commit e9b1a9c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,9 @@ Its attributes and methods include:
| Dimensions | Content | Interface | Mode | Methods |
| -- | -- | -- | -- | -- |
| [**left**][win_layout] | [**background**][win_background] | [**title**][title] | [**visible**][visible] | [on()][win_bind] / [once()][win_bind] |
| [**top**][win_layout] | [**canvas**][win_canvas] | [**cursor**][cursor] | [**fullscreen**][fullscreen] | [off()][win_bind] |
| [**width**][win_layout] | [**ctx**][win_ctx] | [**fit**][fit] | | [close()][close] |
| [**height**][win_layout] | [**page**][win_page] | | | |
| [**top**][win_layout] | [**canvas**][win_canvas] | [**cursor**][cursor] | [**resizable**][resizable] | [off()][win_bind] |
| [**width**][win_layout] | [**ctx**][win_ctx] | [**fit**][fit] | [**fullscreen**][fullscreen] | [close()][close] |
| [**height**][win_layout] | [**page**][win_page] | | | |

[win_background]: #background
[win_canvas]: #canvas-1
Expand All @@ -844,6 +844,7 @@ Its attributes and methods include:
[cursor]: #cursor
[fit]: #fit
[visible]: #visible
[resizable]: #resizable
[fullscreen]: #fullscreen
[win_bind]: #on--off--once
[close]: #close
Expand Down Expand Up @@ -1054,6 +1055,9 @@ When the window is resized, it is likely that it will not perfectly match the as
#### `visible`
When set to `false`, the window will become invisible but will not be permanently ‘closed’. It can be made visible again by setting the property back to `true`.
#### `resizable`
When set to `false`, the window’s size will become fixed and the zoom button in the title bar will be disabled. It can be made user-resizable again by setting the property back to `true`. Note that if the window is set to `fullscreen` its dimensions may still change. If you want to prevent that as well be sure to set up a `keydown` event listener that calls the event’s `preventDefault` on **⌘F** and **Alt-F4** presses so the user can’t switch to fullscreen mode.
#### `fullscreen`
A boolean flag determining whether the window should expand to fill the screen.
Expand Down
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class App extends RustClass{
}

class Window extends EventEmitter{
static #kwargs = "left,top,width,height,title,page,background,fullscreen,cursor,fit,visible".split(/,/)
static #kwargs = "left,top,width,height,title,page,background,fullscreen,cursor,fit,visible,resizable".split(/,/)
#canvas
#state

Expand All @@ -260,6 +260,7 @@ class Window extends EventEmitter{
this.#state = {
title: "",
visible: true,
resizable: true,
background: "white",
fullscreen: false,
page: canvas.pages.length,
Expand Down Expand Up @@ -295,6 +296,9 @@ class Window extends EventEmitter{
get visible(){ return this.#state.visible }
set visible(flag){ this.#state.visible = !!flag }

get resizable(){ return this.#state.resizable }
set resizable(flag){ this.#state.resizable = !!flag }

get fullscreen(){ return this.#state.fullscreen }
set fullscreen(flag){ this.#state.fullscreen = !!flag }

Expand Down
1 change: 1 addition & 0 deletions src/gui/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum CanvasEvent{
Title(String),
Fullscreen(bool),
Visible(bool),
Resizable(bool),
Cursor(Option<CursorIcon>),
Background(Color),
Fit(Fit),
Expand Down
9 changes: 9 additions & 0 deletions src/gui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct WindowSpec {
pub top: Option<f32>,
title: String,
visible: bool,
resizable: bool,
fullscreen: bool,
background: String,
page: u32,
Expand Down Expand Up @@ -69,6 +70,7 @@ impl Window {
.with_transparent(true)
.with_title(spec.title.clone())
.with_visible(false)
.with_resizable(spec.resizable)
.build(&event_loop)
.unwrap();

Expand Down Expand Up @@ -160,6 +162,9 @@ impl Window {
CanvasEvent::Visible(flag) => {
self.handle.set_visible(flag);
}
CanvasEvent::Resizable(flag) => {
self.handle.set_resizable(flag);
}
CanvasEvent::Title(title) => {
self.handle.set_title(&title);
}
Expand Down Expand Up @@ -312,6 +317,10 @@ impl WindowManager {
updates.push(CanvasEvent::Visible(spec.visible));
}

if spec.resizable != win.spec.resizable {
updates.push(CanvasEvent::Resizable(spec.resizable));
}

if spec.fullscreen != win.spec.fullscreen {
updates.push(CanvasEvent::Fullscreen(spec.fullscreen));
}
Expand Down

0 comments on commit e9b1a9c

Please sign in to comment.