use std::{
ops::Deref,
sync::{Arc, RwLock, Weak},
};
use crate::{core::Signal, layout::size::Size, node::color::Color};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WindowInfo {
pub x: i32,
pub y: i32,
pub resolved_width: f32,
pub resolved_height: f32,
pub scale_factor: f32,
pub is_minimized: bool,
pub is_maximized: bool,
pub is_full_screen: bool,
pub is_decorated: bool,
pub is_focused: bool,
pub native_controls: bool,
pub safe_area: SafeAreaInsets,
pub system: SystemSettings,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AppEvent {
Active,
Inactive,
Background,
Foreground,
OpenUrl(String),
Shortcut(String),
Rotated(Orientation),
WindowAsked { external: bool },
GraphicsDeviceLost,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Orientation {
Portrait,
PortraitUpsideDown,
LandscapeLeft,
LandscapeRight,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Orientations(u8);
impl Orientations {
pub const LANDSCAPE: Self = Self(0b1100);
pub const ALL: Self = Self(0b1111);
const fn bit(orientation: Orientation) -> u8 {
match orientation {
Orientation::Portrait => 0b0001,
Orientation::PortraitUpsideDown => 0b0010,
Orientation::LandscapeLeft => 0b0100,
Orientation::LandscapeRight => 0b1000,
}
}
pub fn contains(self, orientation: Orientation) -> bool {
self.0 & Self::bit(orientation) != 0
}
}
impl From<Orientation> for Orientations {
fn from(orientation: Orientation) -> Self {
Self(Self::bit(orientation))
}
}
impl std::ops::BitOr for Orientation {
type Output = Orientations;
fn bitor(self, other: Self) -> Orientations {
Orientations(Orientations::bit(self) | Orientations::bit(other))
}
}
impl std::ops::BitOr<Orientation> for Orientations {
type Output = Self;
fn bitor(self, other: Orientation) -> Self {
Self(self.0 | Self::bit(other))
}
}
impl std::ops::BitOr for Orientations {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl std::ops::BitOr<Orientations> for Orientation {
type Output = Orientations;
fn bitor(self, other: Orientations) -> Orientations {
Orientations(Orientations::bit(self) | other.0)
}
}
type AppEventHandler = Arc<dyn Fn(&AppEvent) + Send + Sync>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Haptic {
Light,
Medium,
Heavy,
Selection,
Success,
Warning,
Error,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SafeAreaInsets {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
pub keyboard: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Appearance {
#[default]
Light,
Dark,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SystemSettings {
pub appearance: Appearance,
pub text_scale: f32,
pub bold_text: bool,
pub reduce_motion: bool,
pub increase_contrast: bool,
pub reduce_transparency: bool,
pub differentiate_without_color: bool,
pub button_shapes: bool,
pub on_off_labels: bool,
}
impl Default for SystemSettings {
fn default() -> Self {
Self {
appearance: Appearance::Light,
text_scale: 1.0,
bold_text: false,
reduce_motion: false,
increase_contrast: false,
reduce_transparency: false,
differentiate_without_color: false,
button_shapes: false,
on_off_labels: false,
}
}
}
impl WindowInfo {
pub fn position(&self) -> (i32, i32) {
(self.x, self.y)
}
pub fn resolved_size(&self) -> Size {
Size::new(self.resolved_width, self.resolved_height)
}
pub fn logical_size(&self) -> Size {
let scale = self.scale_factor.max(f32::EPSILON);
Size::new(self.resolved_width / scale, self.resolved_height / scale)
}
pub fn logical_width(&self) -> f32 {
self.logical_size().width
}
pub fn logical_height(&self) -> f32 {
self.logical_size().height
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WindowResizeDirection {
East,
North,
NorthEast,
NorthWest,
South,
SouthEast,
SouthWest,
West,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WindowCornerRadius {
Default,
None,
Rounded,
RoundedSmall,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WindowBorderColor {
#[default]
Default,
None,
Color(Color),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WindowIcon {
rgba: Vec<u8>,
width: u32,
height: u32,
}
impl WindowIcon {
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Self {
assert_eq!(rgba.len(), (width * height * 4) as usize);
Self { rgba, width, height }
}
#[cfg(feature = "raster")]
pub fn from_image_data(image: &crate::images::ImageData) -> Self {
Self::from_rgba((*image.data_arc()).clone(), image.width(), image.height())
}
pub fn rgba(&self) -> &[u8] {
&self.rgba
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn into_rgba(self) -> (Vec<u8>, u32, u32) {
(self.rgba, self.width, self.height)
}
}
#[cfg(feature = "raster")]
impl From<&WindowIcon> for crate::images::ImageData {
fn from(icon: &WindowIcon) -> Self {
Self::from_rgba(icon.rgba.clone(), icon.width, icon.height)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CloseRequestSource {
Os,
App,
}
pub struct CloseRequest {
window: Weak<RwLock<WindowInner>>,
source: CloseRequestSource,
}
impl CloseRequest {
pub fn source(&self) -> CloseRequestSource {
self.source
}
pub fn proceed(self) {
if let Some(window) = self.window.upgrade() {
let waker = {
let mut inner = window.write().unwrap();
inner.queue_command(WindowCommand::Close);
inner.waker.clone()
};
if let Some(waker) = waker {
waker();
}
}
}
pub fn cancel(self) {}
}
type CloseHandler = Arc<dyn Fn(CloseRequest) + Send + Sync>;
pub(crate) mod controls;
mod handle;
pub(crate) mod parent;
pub(crate) mod resize;
#[cfg(test)]
mod resize_tests;
#[cfg(test)]
pub(crate) mod simulated_window;
#[cfg(feature = "mcp")]
pub(crate) use handle::DialogWindow;
pub use handle::WindowHandle;
use resize::ResizeRequest;
#[cfg(feature = "screenshot")]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScreenshotRegion {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum WindowCommand {
Haptic(Haptic),
Share(crate::app::Share),
Close,
RequestClose(CloseRequestSource),
SetMinimized(bool),
SetMaximized(bool),
SetFullScreen(bool),
SetDecorated(bool),
SetTitle(String),
SetTitleBarColor(Option<Color>),
SetBorderColor(WindowBorderColor),
SetIcon(Option<WindowIcon>),
SetCornerRadius(WindowCornerRadius),
Move {
x: i32,
y: i32,
},
Resize(ResizeRequest),
StartDrag,
StartResize(WindowResizeDirection),
StopDrag,
ShowSystemMenu,
InjectInput(crate::app::synthetic_input::SyntheticInput),
#[cfg(feature = "clipboard")]
PasteFromClipboard {
synthetic: bool,
},
#[cfg(feature = "screenshot")]
Screenshot(std::path::PathBuf, Option<ScreenshotRegion>),
OpenDevtools,
CloseDevtools,
ToggleDevtools,
}
#[derive(Clone)]
pub struct Window {
inner: Arc<RwLock<WindowInner>>,
version_signal: Signal<u64>,
edit_menu: Signal<Option<EditMenuRequest>>,
frames: crate::app::ctx::WindowFrames,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EditMenuRequest {
pub x: f32,
pub y: f32,
pub commands: Vec<crate::app::events::EditCommand>,
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for EditMenuRequest {
fn inspect(&self, formatter: &mut crate::app::component::DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
#[derive(Clone)]
pub(crate) enum NavigationOrigin {
Focus,
Point(f32, f32),
Part(crate::core::ElementRef),
}
pub(crate) type NavigationDone = Box<dyn FnOnce(bool) + Send + Sync>;
pub(crate) struct NavigationRequest {
pub(crate) command: crate::app::events::NavigationCommand,
pub(crate) source: crate::app::events::NavigationSource,
pub(crate) origin: NavigationOrigin,
pub(crate) done: Option<NavigationDone>,
}
pub(crate) type WindowWaker = Arc<dyn Fn() + Send + Sync>;
#[cfg(target_arch = "wasm32")]
thread_local! {
static IN_LOOP_TURN: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
#[cfg(target_arch = "wasm32")]
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_in_loop_turn(in_turn: bool) {
IN_LOOP_TURN.with(|turn| turn.set(in_turn));
}
struct WindowInner {
#[cfg(feature = "mcp")]
accepted_close: bool,
#[cfg(feature = "mcp")]
file_dialogs: crate::mcp::ServingBroker,
#[cfg(feature = "mcp")]
focus_losses: u64,
info: WindowInfo,
corner_radius: WindowCornerRadius,
border_color: WindowBorderColor,
title_bar_inset: f32,
title: Option<String>,
version: u64,
commands: Vec<WindowCommand>,
waker: Option<WindowWaker>,
loop_thread: Option<std::thread::ThreadId>,
shell_attached: bool,
resize_reports: resize::ResizeReports,
parent: Option<parent::ParentWindow>,
close_handler: Option<CloseHandler>,
app_event_handler: Option<AppEventHandler>,
controls: Arc<controls::WindowControlRegions>,
icon: Option<WindowIcon>,
#[cfg(feature = "raster")]
icon_image: Option<crate::images::ImageData>,
copies: Vec<(String, Option<CopyDone>)>,
edits: Vec<(crate::app::events::EditCommand, bool)>,
navigations: Vec<NavigationRequest>,
}
impl WindowInner {
fn queue_command(&mut self, command: WindowCommand) {
#[cfg(feature = "mcp")]
if command == WindowCommand::Close {
self.accepted_close = true;
}
self.commands.push(command);
}
#[cfg_attr(not(feature = "mcp"), allow(dead_code))]
fn queue_resize(&mut self, request: ResizeRequest) -> Option<WindowWaker> {
self.queue_command(WindowCommand::Resize(request));
self.waker.clone()
}
}
impl Default for Window {
fn default() -> Self {
Self::new()
}
}
impl Window {
pub fn new() -> Self {
Self {
inner: Arc::new(RwLock::new(WindowInner {
#[cfg(feature = "mcp")]
accepted_close: false,
#[cfg(feature = "mcp")]
file_dialogs: crate::mcp::ServingBroker::default(),
#[cfg(feature = "mcp")]
focus_losses: 0,
info: WindowInfo {
x: 0,
y: 0,
resolved_width: 0.0,
resolved_height: 0.0,
scale_factor: 1.0,
is_minimized: false,
is_maximized: false,
is_full_screen: false,
is_decorated: true,
is_focused: true,
native_controls: false,
safe_area: SafeAreaInsets::default(),
system: SystemSettings::default(),
},
corner_radius: WindowCornerRadius::Default,
border_color: WindowBorderColor::Default,
title_bar_inset: 0.0,
title: None,
version: 0,
commands: Vec::new(),
waker: None,
loop_thread: None,
shell_attached: false,
resize_reports: resize::ResizeReports::default(),
parent: None,
close_handler: None,
app_event_handler: None,
controls: Arc::default(),
icon: None,
#[cfg(feature = "raster")]
icon_image: None,
copies: Vec::new(),
edits: Vec::new(),
navigations: Vec::new(),
})),
version_signal: Signal::new(0),
edit_menu: Signal::new(None),
frames: crate::app::ctx::WindowFrames::default(),
}
}
#[cfg_attr(not(all(feature = "winit", windows)), allow(dead_code))]
pub(crate) fn set_parent(&self, parent: Option<parent::ParentWindow>) {
self.inner.write().unwrap().parent = parent;
}
pub(crate) fn parent(&self) -> Option<parent::ParentWindow> {
self.inner.read().unwrap().parent
}
#[cfg(feature = "mcp")]
pub(crate) fn serve_file_dialogs(&self, broker: &crate::mcp::ServingBroker) {
if self.inner.read().unwrap().file_dialogs.is(broker) {
return;
}
self.inner.write().unwrap().file_dialogs = broker.clone();
}
#[cfg(feature = "mcp")]
pub(crate) fn file_dialog_broker(&self) -> Option<crate::mcp::FileDialogBroker> {
self.inner.read().unwrap().file_dialogs.broker()
}
pub(crate) fn set_app_event_handler(&self, handler: Option<AppEventHandler>) {
self.inner.write().unwrap().app_event_handler = handler;
}
#[cfg_attr(not(target_os = "ios"), allow(dead_code))]
pub(crate) fn has_app_event_handler(&self) -> bool {
self.inner.read().unwrap().app_event_handler.is_some()
}
pub(crate) fn dispatch_app_event(&self, event: &AppEvent) {
let handler = self.inner.read().unwrap().app_event_handler.clone();
if let Some(handler) = handler {
handler(event);
}
}
pub(crate) fn dispatch_close_request(&self, source: CloseRequestSource) {
let handler = self.inner.read().unwrap().close_handler.clone();
let request = CloseRequest {
window: Arc::downgrade(&self.inner),
source,
};
if let Some(handler) = handler {
handler(request);
} else {
request.proceed();
}
}
#[cfg(feature = "mcp")]
pub(crate) fn close_queued(&self) -> bool {
self.inner.read().unwrap().commands.contains(&WindowCommand::Close)
}
#[cfg(feature = "mcp")]
pub(crate) fn close_accepted(&self) -> bool {
self.inner.read().unwrap().accepted_close
}
#[cfg(feature = "mcp")]
pub(crate) fn focus_losses(&self) -> u64 {
self.inner.read().unwrap().focus_losses
}
pub(crate) fn set_icon(&self, icon: Option<WindowIcon>) {
let (version, waker) = {
let mut inner = self.inner.write().unwrap();
if inner.icon == icon {
return;
}
#[cfg(feature = "raster")]
{
inner.icon_image = icon.as_ref().map(crate::images::ImageData::from);
}
inner.icon = icon;
(Self::bump_version(&mut inner), inner.waker.clone())
};
self.version_signal.set(version);
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn request_copy(&self, text: String) {
self.push_copy(text, None);
}
pub(crate) fn request_copy_then(&self, text: String, done: impl FnOnce(bool) + Send + Sync + 'static) {
self.push_copy(text, Some(Box::new(done)));
}
fn push_copy(&self, text: String, done: Option<CopyDone>) {
let waker = {
let mut inner = self.inner.write().unwrap();
inner.copies.push((text, done));
inner.waker.clone()
};
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn take_copies(&self) -> Vec<(String, Option<CopyDone>)> {
std::mem::take(&mut self.inner.write().unwrap().copies)
}
pub(crate) fn request_edit(&self, command: crate::app::events::EditCommand) {
let synthetic = crate::app::events::is_synthetic_input();
let waker = {
let mut inner = self.inner.write().unwrap();
inner.edits.push((command, synthetic));
inner.waker.clone()
};
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn has_requests(&self) -> bool {
let inner = self.inner.read().unwrap();
!inner.edits.is_empty() || !inner.navigations.is_empty() || !inner.copies.is_empty()
}
pub(crate) fn take_edits(&self) -> Vec<(crate::app::events::EditCommand, bool)> {
std::mem::take(&mut self.inner.write().unwrap().edits)
}
pub(crate) fn request_navigation(&self, request: NavigationRequest) {
let waker = {
let mut inner = self.inner.write().unwrap();
inner.navigations.push(request);
inner.waker.clone()
};
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn take_navigations(&self) -> Vec<NavigationRequest> {
std::mem::take(&mut self.inner.write().unwrap().navigations)
}
pub(crate) fn icon(&self) -> Option<WindowIcon> {
self.inner.read().unwrap().icon.clone()
}
#[cfg(feature = "raster")]
pub(crate) fn icon_image(&self) -> Option<crate::images::ImageData> {
self.inner.read().unwrap().icon_image.clone()
}
pub(crate) fn track_access(&self) {
let _ = self.version_signal.get();
}
pub(crate) fn info(&self) -> WindowInfo {
self.inner.read().unwrap().info
}
#[allow(dead_code)]
pub(crate) fn corner_radius(&self) -> WindowCornerRadius {
self.inner.read().unwrap().corner_radius
}
pub(crate) fn frames(&self) -> &crate::app::ctx::WindowFrames {
&self.frames
}
pub(crate) fn handle(&self) -> WindowHandle {
WindowHandle {
info: self.info(),
window: self.clone(),
}
}
pub(crate) fn record_initial_title(&self, title: &str) {
let mut inner = self.inner.write().unwrap();
if inner.title.is_none() {
inner.title = Some(title.to_owned());
}
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn take_commands(&self) -> Vec<WindowCommand> {
std::mem::take(&mut self.inner.write().unwrap().commands)
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn take_shell_commands(&self) -> Vec<WindowCommand> {
let mut result = Vec::new();
for command in self.take_commands() {
if let WindowCommand::RequestClose(source) = command {
self.dispatch_close_request(source);
} else {
result.push(command);
}
}
for command in self.take_commands() {
if matches!(command, WindowCommand::RequestClose(_)) {
self.push_command(command);
} else {
result.push(command);
}
}
result
}
#[cfg(all(feature = "winit", target_os = "macos"))]
pub(crate) fn requeue_command(&self, command: WindowCommand) {
self.push_command(command);
}
fn push_command(&self, command: WindowCommand) {
let waker = {
let mut inner = self.inner.write().unwrap();
inner.queue_command(command);
inner.waker.clone()
};
if let Some(waker) = waker {
waker();
}
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_waker(&self, waker: WindowWaker) {
let mut inner = self.inner.write().unwrap();
inner.waker = Some(waker);
inner.loop_thread = Some(std::thread::current().id());
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn attach_shell(&self, waker: WindowWaker) {
self.set_waker(waker);
self.inner.write().unwrap().shell_attached = true;
}
pub(crate) fn wake(&self) {
let waker = self.inner.read().unwrap().waker.clone();
if let Some(waker) = waker {
waker();
}
}
pub(crate) fn loop_waker(&self) -> WindowWaker {
let window = Arc::downgrade(&self.inner);
Arc::new(move || {
let wake = window.upgrade().and_then(|window| window.read().unwrap().waker.clone());
if let Some(wake) = wake {
wake();
}
})
}
pub(crate) fn off_loop_waker(&self) -> WindowWaker {
let window = Arc::downgrade(&self.inner);
Arc::new(move || {
let wake = window.upgrade().and_then(|window| {
let window = window.read().unwrap();
let in_turn = window.loop_thread == Some(std::thread::current().id());
#[cfg(target_arch = "wasm32")]
let in_turn = in_turn && IN_LOOP_TURN.with(std::cell::Cell::get);
if in_turn { None } else { window.waker.clone() }
});
if let Some(wake) = wake {
wake();
}
})
}
pub fn version(&self) -> u64 {
self.inner.read().unwrap().version
}
pub(crate) fn set_resolved_size(&self, width: f32, height: f32) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.resolved_width == width && inner.info.resolved_height == height {
return;
}
inner.info.resolved_width = width;
inner.info.resolved_height = height;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
pub(crate) fn set_scale_factor(&self, scale_factor: f32) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.scale_factor == scale_factor {
return;
}
inner.info.scale_factor = scale_factor;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
pub(crate) fn set_position(&self, x: i32, y: i32) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.x == x && inner.info.y == y {
return;
}
inner.info.x = x;
inner.info.y = y;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_minimized(&self, minimized: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.is_minimized == minimized {
return;
}
inner.info.is_minimized = minimized;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_maximized(&self, maximized: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.is_maximized == maximized {
return;
}
inner.info.is_maximized = maximized;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_full_screen(&self, full_screen: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.is_full_screen == full_screen {
return;
}
inner.info.is_full_screen = full_screen;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(all(feature = "winit", target_os = "macos")), allow(dead_code))]
pub(crate) fn set_native_controls(&self, native: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.native_controls == native {
return;
}
inner.info.native_controls = native;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(all(feature = "winit", target_os = "macos")), allow(dead_code))]
pub(crate) fn control_regions(&self) -> Arc<controls::WindowControlRegions> {
self.inner.read().unwrap().controls.clone()
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_decorated(&self, decorated: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.is_decorated == decorated {
return;
}
inner.info.is_decorated = decorated;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
pub fn edit_menu(&self) -> Option<EditMenuRequest> {
self.edit_menu.get()
}
pub fn edit_menu_signal(&self) -> Signal<Option<EditMenuRequest>> {
self.edit_menu.clone()
}
pub fn set_edit_menu(&self, menu: Option<EditMenuRequest>) {
if self.edit_menu.get_untracked() != menu {
self.edit_menu.set(menu);
}
}
pub fn set_safe_area(&self, safe_area: SafeAreaInsets) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.safe_area == safe_area {
return;
}
inner.info.safe_area = safe_area;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
pub fn set_system_settings(&self, system: SystemSettings) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.system == system {
return;
}
inner.info.system = system;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_focused(&self, focused: bool) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.info.is_focused == focused {
return;
}
inner.info.is_focused = focused;
#[cfg(feature = "mcp")]
if !focused {
inner.focus_losses += 1;
}
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_corner_radius(&self, radius: WindowCornerRadius) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.corner_radius == radius {
return;
}
inner.corner_radius = radius;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
pub(crate) fn set_control_regions(&self, regions: controls::WindowControlRegions) {
let mut inner = self.inner.write().unwrap();
if *inner.controls != regions {
inner.controls = Arc::new(regions);
}
}
#[cfg_attr(not(all(feature = "winit", windows)), allow(dead_code))]
pub(crate) fn control_at(&self, x: f32, y: f32) -> Option<crate::node::WindowControl> {
let (regions, scale) = {
let inner = self.inner.read().unwrap();
if inner.controls.is_empty() {
return None;
}
(inner.controls.clone(), inner.info.scale_factor.max(0.01))
};
regions.at(x / scale, y / scale)
}
pub(crate) fn set_title_bar_inset(&self, inset: f32) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.title_bar_inset == inset {
return;
}
inner.title_bar_inset = inset;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
#[cfg_attr(not(feature = "winit"), allow(dead_code))]
pub(crate) fn set_border_color(&self, color: WindowBorderColor) {
let version = {
let mut inner = self.inner.write().unwrap();
if inner.border_color == color {
return;
}
inner.border_color = color;
Self::bump_version(&mut inner)
};
self.version_signal.set(version);
}
fn bump_version(inner: &mut WindowInner) -> u64 {
inner.version = inner.version.wrapping_add(1);
inner.version
}
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod orientation_tests {
use super::{Orientation, Orientations};
#[test]
fn orientations_joined_with_or_are_the_ones_named() {
let allowed = Orientation::Portrait | Orientation::LandscapeLeft;
assert!(allowed.contains(Orientation::Portrait));
assert!(allowed.contains(Orientation::LandscapeLeft));
assert!(!allowed.contains(Orientation::LandscapeRight));
assert!(!allowed.contains(Orientation::PortraitUpsideDown));
let more = allowed | Orientation::LandscapeRight;
assert_eq!(more, Orientation::Portrait | Orientations::LANDSCAPE);
assert_eq!(more | Orientation::PortraitUpsideDown, Orientations::ALL);
assert_eq!(
Orientations::from(Orientation::Portrait) | Orientations::LANDSCAPE,
more
);
}
}
pub(crate) type CopyDone = Box<dyn FnOnce(bool) + Send + Sync>;