Skip to content

Commit

Permalink
Add no-frame property for a borderless/frameless window
Browse files Browse the repository at this point in the history
  • Loading branch information
rybertm authored and ogoffart committed Oct 11, 2021
1 parent 503a9b3 commit c3c7765
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/builtin_elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ or smaller. The initial width can be controlled with the `preferred-width` prope

* **`title`** (*string*): The window title that is shown in the title bar.
* **`icon`** (*image*): The window icon shown in the title bar or the task bar on window managers supporting it.
* **`no-frame`** (*bool*): Whether the window should be borderless/frameless or not.
* **`background`** (*color*): The background color of the Window. (default value: depends on the style)
* **`default-font-family`** (*string*): The font family to use as default in text elements inside this window, that don't
have their family set.
Expand Down
1 change: 1 addition & 0 deletions examples/printerdemo/ui/printerdemo.60
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ MainWindow := Window {
width: 772px;
height: 504px;
title: "SixtyFPS printer demo";
no-frame: false;
background: DemoPalette.main-background;
default-font-family: "Noto Sans";
default-font-size: DemoPalette.base-font-size;
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_compiler/builtins.60
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ WindowItem := _ {
property <color> background; // StyleMetrics.window_background set in apply_default_properties_from_style
property <color> color <=> background;
property <string> title: "SixtyFPS Window";
property <bool> no-frame: false;
property <string> default-font-family;
property <length> default-font-size;
property <int> default-font-weight;
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_runtime/corelib/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,7 @@ pub struct WindowItem {
pub height: Property<f32>,
pub background: Property<Color>,
pub title: Property<SharedString>,
pub no_frame: Property<bool>,
pub icon: Property<crate::graphics::Image>,
pub default_font_family: Property<SharedString>,
pub default_font_size: Property<f32>,
Expand Down
15 changes: 12 additions & 3 deletions sixtyfps_runtime/rendering_backends/gl/graphics_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ impl GraphicsWindow {
let component = ComponentRc::borrow_pin(&component);
let root_item = component.as_ref().get_item_ref(0);

let window_title = if let Some(window_item) =
let (window_title, no_frame) = if let Some(window_item) =
ItemRef::downcast_pin::<corelib::items::WindowItem>(root_item)
{
window_item.title().to_string()
(window_item.title().to_string(), window_item.no_frame())
} else {
"SixtyFPS Window".to_string()
("SixtyFPS Window".to_string(), false)
};
let window_builder = winit::window::WindowBuilder::new().with_title(window_title);

Expand All @@ -124,6 +124,9 @@ impl GraphicsWindow {
}
};

let window_builder =
if no_frame { window_builder.with_decorations(false) } else { window_builder };

#[cfg(target_arch = "wasm32")]
let (opengl_context, renderer) =
crate::OpenGLContext::new_context_and_renderer(window_builder, &self.canvas_id);
Expand Down Expand Up @@ -380,6 +383,7 @@ impl PlatformWindow for GraphicsWindow {
fn apply_window_properties(&self, window_item: Pin<&sixtyfps_corelib::items::WindowItem>) {
let background = window_item.background();
let title = window_item.title();
let no_frame = window_item.no_frame();
let icon = window_item.icon();
let width = window_item.width();
let height = window_item.height();
Expand All @@ -395,6 +399,11 @@ impl PlatformWindow for GraphicsWindow {
let window = self.borrow_mapped_window().unwrap();
let winit_window = window.opengl_context.window();
winit_window.set_title(&title);
if no_frame {
winit_window.set_decorations(false);
} else {
winit_window.set_decorations(true);
}
if let Some(rgba) = crate::IMAGE_CACHE
.with(|c| c.borrow_mut().load_image_resource((&icon).into()))
.and_then(|i| i.to_rgba())
Expand Down
14 changes: 14 additions & 0 deletions sixtyfps_runtime/rendering_backends/qt/qt_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,7 @@ impl PlatformWindow for QtWindow {
fn apply_window_properties(&self, window_item: Pin<&items::WindowItem>) {
let widget_ptr = self.widget_ptr();
let title: qttypes::QString = window_item.title().as_str().into();
let no_frame = window_item.no_frame();
let mut size = qttypes::QSize {
width: window_item.width().ceil() as _,
height: window_item.height().ceil() as _,
Expand Down Expand Up @@ -1296,6 +1297,19 @@ impl PlatformWindow for QtWindow {
pal.setColor(QPalette::Window, QColor::fromRgba(background));
widget_ptr->setPalette(pal);
}};

if no_frame {
cpp! {unsafe [widget_ptr as "QWidget*"] {
widget_ptr->setWindowFlags(Qt::FramelessWindowHint);
widget_ptr->show();
}};
} else {
cpp! {unsafe [widget_ptr as "QWidget*"] {
auto flags = widget_ptr->windowFlags();
widget_ptr->setWindowFlags(flags & (~Qt::FramelessWindowHint));
widget_ptr->show();
}};
}
}

/// Set the min/max sizes on the QWidget
Expand Down

0 comments on commit c3c7765

Please sign in to comment.