Skip to content

Commit

Permalink
Added set_fullscreen API to WindowAdapter.
Browse files Browse the repository at this point in the history
See #3283
  • Loading branch information
qhua948 committed Jan 8, 2024
1 parent 692a8bc commit 991b11a
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -29,6 +29,7 @@ members = [
'examples/energy-monitor',
'examples/mcu-board-support',
'examples/uefi-demo',
'examples/fullscreen_toggle',
'helper_crates/const-field-offset',
'helper_crates/vtable',
'helper_crates/vtable/macro',
Expand Down
1 change: 0 additions & 1 deletion docker/Dockerfile.torizon-demos
Expand Up @@ -53,6 +53,5 @@ RUN apt-get update \

COPY --from=build /slint/demos/* /usr/bin/

ENV SLINT_FULLSCREEN=1
ENV SLINT_BACKEND=winit-skia
CMD /usr/bin/printerdemo
7 changes: 0 additions & 7 deletions docs/reference/src/advanced/backend_qt.md
Expand Up @@ -10,10 +10,3 @@ macOS, Windows, Linux with Wayland and X11, and direct full-screen rendering via
The Qt backend only supports software rendering at the moment. That means it runs with any graphics driver,
but it does not utilize GPU hardware acceleration.

## Configuration Options

The Qt backend reads and interprets the following environment variables:

| Name | Accepted Values | Description |
|--------------------|-----------------|--------------------------------------------------------------------|
| `SLINT_FULLSCREEN` | any value | If this variable is set, every window is shown in fullscreen mode. |
8 changes: 0 additions & 8 deletions docs/reference/src/advanced/backend_winit.md
Expand Up @@ -17,11 +17,3 @@ The Winit backend supports different renderers. They can be explicitly selected
| Skia Software | Software-only rendering with Skia | `winit-skia-software` |
| software | Software-rendering, no GPU required | `winit-software` |


## Configuration Options

The Winit backend reads and interprets the following environment variables:

| Name | Accepted Values | Description |
|--------------------|-----------------|--------------------------------------------------------------------|
| `SLINT_FULLSCREEN` | any value | If this variable is set, every window is shown in fullscreen mode. |
13 changes: 13 additions & 0 deletions examples/fullscreen_toggle/CMakeLists.txt
@@ -0,0 +1,13 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: MIT

cmake_minimum_required(VERSION 3.21)
project(fullscreen_toggle LANGUAGES CXX)

if (NOT TARGET Slint::Slint)
find_package(Slint REQUIRED)
endif()

add_executable(fullscreen_toggle fullscreen_toggle.cpp)
target_link_libraries(fullscreen_toggle PRIVATE Slint::Slint)
slint_target_sources(fullscreen_toggle fullscreen_toggle.slint)
21 changes: 21 additions & 0 deletions examples/fullscreen_toggle/Cargo.toml
@@ -0,0 +1,21 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: MIT

[package]
name = "fullscreen_toggle"
version = "1.4.0"
authors = ["Slint Developers <info@slint.dev>"]
edition = "2021"
publish = false
license = "MIT"

[[bin]]
path = "main.rs"
name = "fullscreen_toggle"

[dependencies]
slint = { path = "../../api/rs/slint" }

[build-dependencies]
slint-build = { path = "../../api/rs/build" }

18 changes: 18 additions & 0 deletions examples/fullscreen_toggle/fullscreen_toggle.cpp
@@ -0,0 +1,18 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

#include "fullscreen_toggle.h"
#include <random>

int main()
{
auto app_window = AppWindow::create();

Check failure on line 9 in examples/fullscreen_toggle/fullscreen_toggle.cpp

View workflow job for this annotation

GitHub Actions / docs / docs

[formatters] reported by reviewdog 🐶 Raw Output: examples/fullscreen_toggle/fullscreen_toggle.cpp:9:- auto app_window = AppWindow::create(); examples/fullscreen_toggle/fullscreen_toggle.cpp:10:- bool fullscreen = false; examples/fullscreen_toggle/fullscreen_toggle.cpp:11:- app_window.on_fullscreen_toggle([fullscreen, main_window_weak = slint::ComponentWeakHandle(main_window)] { examples/fullscreen_toggle/fullscreen_toggle.cpp:12:- auto main_window = *main_window_weak.lock(); examples/fullscreen_toggle/fullscreen_toggle.cpp:13:- fullscreen = !fullscreen; examples/fullscreen_toggle/fullscreen_toggle.cpp:14:- main_window.window().set_fullscreen(fullscreen); examples/fullscreen_toggle/fullscreen_toggle.cpp:15:- }); examples/fullscreen_toggle/fullscreen_toggle.cpp:9:+ auto app_window = AppWindow::create(); examples/fullscreen_toggle/fullscreen_toggle.cpp:10:+ bool fullscreen = false; examples/fullscreen_toggle/fullscreen_toggle.cpp:11:+ app_window.on_fullscreen_toggle( examples/fullscreen_toggle/fullscreen_toggle.cpp:12:+ [fullscreen, main_window_weak = slint::ComponentWeakHandle(main_window)] { examples/fullscreen_toggle/fullscreen_toggle.cpp:13:+ auto main_window = *main_window_weak.lock(); examples/fullscreen_toggle/fullscreen_toggle.cpp:14:+ fullscreen = !fullscreen; examples/fullscreen_toggle/fullscreen_toggle.cpp:15:+ main_window.window().set_fullscreen(fullscreen); examples/fullscreen_toggle/fullscreen_toggle.cpp:16:+ });
bool fullscreen = false;
app_window.on_fullscreen_toggle([fullscreen, main_window_weak = slint::ComponentWeakHandle(main_window)] {
auto main_window = *main_window_weak.lock();
fullscreen = !fullscreen;
main_window.window().set_fullscreen(fullscreen);
});

main_window->run();

Check failure on line 17 in examples/fullscreen_toggle/fullscreen_toggle.cpp

View workflow job for this annotation

GitHub Actions / docs / docs

[formatters] reported by reviewdog 🐶 Raw Output: examples/fullscreen_toggle/fullscreen_toggle.cpp:17:- main_window->run(); examples/fullscreen_toggle/fullscreen_toggle.cpp:18:+ main_window->run();
}
20 changes: 20 additions & 0 deletions examples/fullscreen_toggle/fullscreen_toggle.slint
@@ -0,0 +1,20 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

import { Button, VerticalBox } from "std-widgets.slint";

export component AppWindow inherits Window {
callback fullscreen-toggle();
VerticalBox {
Text {
text: "Toggle Button";
}
Button {
text: "Toggle fullscreen";
clicked => {
root.fullscreen-toggle();
}
}
}
}

20 changes: 20 additions & 0 deletions examples/fullscreen_toggle/main.rs
@@ -0,0 +1,20 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

slint::slint! {
import { AppWindow } from "fullscreen_toggle.slint";
}

fn main() -> Result<(), slint::PlatformError> {
let ui = AppWindow::new()?;
let mut fullscreen = false;

let ui_handle = ui.as_weak();
ui.on_fullscreen_toggle(move || {
let ui = ui_handle.unwrap();
fullscreen = !fullscreen;
ui.window().set_fullscreen(fullscreen);
});

ui.run()
}
18 changes: 13 additions & 5 deletions internal/backends/qt/qt_window.rs
Expand Up @@ -1530,11 +1530,7 @@ impl WindowAdapter for QtWindow {
fn set_visible(&self, visible: bool) -> Result<(), PlatformError> {
if visible {
let widget_ptr = self.widget_ptr();
let fullscreen = std::env::var("SLINT_FULLSCREEN").is_ok();
cpp! {unsafe [widget_ptr as "QWidget*", fullscreen as "bool"] {
if (fullscreen) {
widget_ptr->setWindowState(Qt::WindowFullScreen);
}
cpp! {unsafe [widget_ptr as "QWidget*"] {
widget_ptr->show();
}};
let qt_platform_name = cpp! {unsafe [] -> qttypes::QString as "QString" {
Expand Down Expand Up @@ -1702,6 +1698,18 @@ impl WindowAdapter for QtWindow {
}};
}

fn set_fullscreen(&self, fullscreen: bool) {
let widget_ptr = self.widget_ptr();
cpp! {unsafe [widget_ptr as "QWidget*", fullscreen as "bool"] {
if (fullscreen) {
widget_ptr->setWindowState(Qt::WindowFullScreen);
} else {
widget_ptr->setWindowState(Qt::WindowNoState);
}
}
};
}

fn internal(&self, _: i_slint_core::InternalToken) -> Option<&dyn WindowAdapterInternal> {
Some(self)
}
Expand Down
13 changes: 8 additions & 5 deletions internal/backends/winit/winitwindowadapter.rs
Expand Up @@ -203,11 +203,6 @@ impl WinitWindowAdapter {
let mut window_builder =
winit::window::WindowBuilder::new().with_transparent(true).with_visible(false);

if std::env::var("SLINT_FULLSCREEN").is_ok() {
window_builder =
window_builder.with_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
}

window_builder = window_builder.with_title("Slint Window".to_string());

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -579,6 +574,14 @@ impl WindowAdapter for WinitWindowAdapter {
});
}

fn set_fullscreen(&self, fullscreen: bool) {
if fullscreen {
self.winit_window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
} else {
self.winit_window.set_fullscreen(None);
}
}

fn internal(&self, _: corelib::InternalToken) -> Option<&dyn WindowAdapterInternal> {
Some(self)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/core/api.rs
Expand Up @@ -446,6 +446,11 @@ impl Window {
crate::window::WindowAdapter::set_size(&*self.0.window_adapter(), size);
}

/// Set or unset the window to display fullscreen.
pub fn set_fullscreen(&self, fullscreen: bool) {
self.0.window_adapter().set_fullscreen(fullscreen);
}

/// Dispatch a window event to the scene.
///
/// Use this when you're implementing your own backend and want to forward user input events.
Expand Down
3 changes: 3 additions & 0 deletions internal/core/window.rs
Expand Up @@ -132,6 +132,9 @@ pub trait WindowAdapter {
/// be called again.
fn update_window_properties(&self, _properties: WindowProperties<'_>) {}

/// Set to display the window as fullscreen.
fn set_fullscreen(&self, _fullscreen: bool) {}

#[doc(hidden)]
fn internal(&self, _: crate::InternalToken) -> Option<&dyn WindowAdapterInternal> {
None
Expand Down
4 changes: 0 additions & 4 deletions tools/lsp/main.rs
Expand Up @@ -239,10 +239,6 @@ fn main() {
if !args.backend.is_empty() {
std::env::set_var("SLINT_BACKEND", &args.backend);
}
if args.fullscreen {
// TODO: Have an API to set the Window fullscreen #3283
std::env::set_var("SLINT_FULLSCREEN", "1");
}

#[cfg(feature = "preview-engine")]
{
Expand Down
6 changes: 4 additions & 2 deletions tools/lsp/preview/native.rs
Expand Up @@ -150,7 +150,7 @@ pub fn open_ui(sender: &ServerNotifier) {
}

fn open_ui_impl(preview_state: &mut PreviewState) {
let (default_style, show_preview_ui) = {
let (default_style, show_preview_ui, fullscreen) = {
let cache = super::CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
let style = cache.config.style.clone();
let style = if style.is_empty() {
Expand All @@ -163,12 +163,14 @@ fn open_ui_impl(preview_state: &mut PreviewState) {
.hide_ui
.or_else(|| CLI_ARGS.with(|args| args.get().map(|a| a.no_toolbar.clone())))
.unwrap_or(false);
(style, !hide_ui)
let fullscreen = CLI_ARGS.with(|args| args.get().map(|a| a.fullscreen).unwrap_or_default());
(style, !hide_ui, fullscreen)
};

// TODO: Handle Error!
let ui = preview_state.ui.get_or_insert_with(|| super::ui::create_ui(default_style).unwrap());
ui.set_show_preview_ui(show_preview_ui);
ui.window().set_fullscreen(fullscreen);
ui.window().on_close_requested(|| {
let mut cache = super::CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
cache.ui_is_visible = false;
Expand Down

0 comments on commit 991b11a

Please sign in to comment.