Merge branch 'master' into feat/multi-window-support

This commit is contained in:
Héctor Ramón Jiménez 2023-11-29 22:28:31 +01:00
commit e09b4e24dd
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
331 changed files with 12085 additions and 3976 deletions

View file

@ -1,14 +1,14 @@
[package]
name = "iced_winit"
version = "0.9.1"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2021"
description = "A winit runtime for Iced"
license = "MIT"
repository = "https://github.com/iced-rs/iced"
documentation = "https://docs.rs/iced_winit"
keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"]
description = "A runtime for iced on top of winit"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
categories.workspace = true
keywords.workspace = true
[features]
default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"]
@ -22,36 +22,23 @@ wayland-csd-adwaita = ["winit/wayland-csd-adwaita"]
multi-window = ["iced_runtime/multi-window"]
[dependencies]
window_clipboard = "0.3"
log = "0.4"
thiserror = "1.0"
raw-window-handle = "0.5"
iced_graphics.workspace = true
iced_runtime.workspace = true
iced_style.workspace = true
[dependencies.winit]
version = "0.28"
git = "https://github.com/iced-rs/winit.git"
rev = "c52db2045d0a2f1b8d9923870de1d4ab1994146e"
default-features = false
log.workspace = true
raw-window-handle.workspace = true
thiserror.workspace = true
tracing.workspace = true
window_clipboard.workspace = true
winit.workspace = true
[dependencies.iced_runtime]
version = "0.1"
path = "../runtime"
sysinfo.workspace = true
sysinfo.optional = true
[dependencies.iced_graphics]
version = "0.8"
path = "../graphics"
[target.'cfg(target_os = "windows")'.dependencies]
winapi.workspace = true
[dependencies.iced_style]
version = "0.8"
path = "../style"
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3.6"
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3"
features = ["Document", "Window"]
[dependencies.sysinfo]
version = "0.28"
optional = true
[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys.workspace = true
web-sys.features = ["Document", "Window"]

View file

@ -18,7 +18,6 @@ use crate::runtime::clipboard;
use crate::runtime::program::Program;
use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::{Command, Debug};
use crate::settings;
use crate::style::application::{Appearance, StyleSheet};
use crate::{Clipboard, Error, Proxy, Settings};
@ -138,7 +137,7 @@ where
let should_be_visible = settings.window.visible;
let exit_on_close_request = settings.window.exit_on_close_request;
let builder = settings::window_builder(
let builder = conversion::window_settings(
settings.window,
&application.title(),
event_loop.primary_monitor(),
@ -146,7 +145,7 @@ where
)
.with_visible(false);
log::debug!("Window builder: {:#?}", builder);
log::debug!("Window builder: {builder:#?}");
let window = builder
.build(&event_loop)
@ -163,7 +162,7 @@ where
let body = document.body().unwrap();
let target = target.and_then(|target| {
body.query_selector(&format!("#{}", target))
body.query_selector(&format!("#{target}"))
.ok()
.unwrap_or(None)
});
@ -182,7 +181,14 @@ where
};
}
let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;
let (compositor, mut renderer) =
C::new(compositor_settings, Some(&window))?;
for font in settings.fonts {
use crate::core::text::Renderer;
renderer.load_font(font);
}
let (mut event_sender, event_receiver) = mpsc::unbounded();
let (control_sender, mut control_receiver) = mpsc::unbounded();
@ -691,6 +697,9 @@ pub fn run_command<A, C, E>(
command::Action::Future(future) => {
runtime.spawn(future);
}
command::Action::Stream(stream) => {
runtime.run(stream);
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read());
@ -729,7 +738,7 @@ pub fn run_command<A, C, E>(
size.width,
size.height,
)))
.expect("Send message to event loop")
.expect("Send message to event loop");
}
window::Action::Maximize(maximized) => {
window.set_maximized(maximized);
@ -751,7 +760,7 @@ pub fn run_command<A, C, E>(
));
}
window::Action::ChangeIcon(icon) => {
window.set_window_icon(conversion::icon(icon))
window.set_window_icon(conversion::icon(icon));
}
window::Action::FetchMode(tag) => {
let mode = if window.is_visible().unwrap_or(true) {
@ -765,7 +774,7 @@ pub fn run_command<A, C, E>(
.expect("Send message to event loop");
}
window::Action::ToggleMaximize => {
window.set_maximized(!window.is_maximized())
window.set_maximized(!window.is_maximized());
}
window::Action::ToggleDecorations => {
window.set_decorations(!window.is_decorated());
@ -800,7 +809,7 @@ pub fn run_command<A, C, E>(
bytes,
state.physical_size(),
)))
.expect("Send message to event loop.")
.expect("Send message to event loop.");
}
},
command::Action::System(action) => match action {
@ -818,7 +827,7 @@ pub fn run_command<A, C, E>(
proxy
.send_event(message)
.expect("Send message to event loop")
.expect("Send message to event loop");
});
}
}

View file

@ -184,9 +184,7 @@ where
/// window.
///
/// Normally an [`Application`] should be synchronized with its [`State`]
/// and window after calling [`Application::update`].
///
/// [`Application::update`]: crate::Program::update
/// and window after calling [`crate::application::update`].
pub fn synchronize(&mut self, application: &A, window: &Window) {
// Update window title
let new_title = application.title();

View file

@ -45,7 +45,7 @@ impl Clipboard {
State::Connected(clipboard) => match clipboard.write(contents) {
Ok(()) => {}
Err(error) => {
log::warn!("error writing to clipboard: {}", error)
log::warn!("error writing to clipboard: {error}");
}
},
State::Unavailable => {}
@ -59,6 +59,6 @@ impl crate::core::Clipboard for Clipboard {
}
fn write(&mut self, contents: String) {
self.write(contents)
self.write(contents);
}
}

View file

@ -1,13 +1,123 @@
//! Convert [`winit`] types into [`iced_native`] types, and viceversa.
//! Convert [`winit`] types into [`iced_runtime`] types, and viceversa.
//!
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
//! [`iced_runtime`]: https://github.com/iced-rs/iced/tree/0.10/runtime
use crate::core::keyboard;
use crate::core::mouse;
use crate::core::touch;
use crate::core::window;
use crate::core::{Event, Point};
/// Converts some [`window::Settings`] into a `WindowBuilder` from `winit`.
pub fn window_settings(
settings: window::Settings,
title: &str,
primary_monitor: Option<winit::monitor::MonitorHandle>,
_id: Option<String>,
) -> winit::window::WindowBuilder {
let mut window_builder = winit::window::WindowBuilder::new();
let (width, height) = settings.size;
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_resizable(settings.resizable)
.with_enabled_buttons(if settings.resizable {
winit::window::WindowButtons::all()
} else {
winit::window::WindowButtons::CLOSE
| winit::window::WindowButtons::MINIMIZE
})
.with_decorations(settings.decorations)
.with_transparent(settings.transparent)
.with_window_icon(settings.icon.and_then(icon))
.with_window_level(window_level(settings.level))
.with_visible(settings.visible);
if let Some(position) =
position(primary_monitor.as_ref(), settings.size, settings.position)
{
window_builder = window_builder.with_position(position);
}
if let Some((width, height)) = settings.min_size {
window_builder = window_builder
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
}
if let Some((width, height)) = settings.max_size {
window_builder = window_builder
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
}
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
// `with_name` is available on both `WindowBuilderExtWayland` and `WindowBuilderExtX11` and they do
// exactly the same thing. We arbitrarily choose `WindowBuilderExtWayland` here.
use ::winit::platform::wayland::WindowBuilderExtWayland;
if let Some(id) = _id {
window_builder = window_builder.with_name(id.clone(), id);
}
}
#[cfg(target_os = "windows")]
{
use winit::platform::windows::WindowBuilderExtWindows;
#[allow(unsafe_code)]
unsafe {
window_builder = window_builder
.with_parent_window(settings.platform_specific.parent);
}
window_builder = window_builder
.with_drag_and_drop(settings.platform_specific.drag_and_drop);
}
#[cfg(target_os = "macos")]
{
use winit::platform::macos::WindowBuilderExtMacOS;
window_builder = window_builder
.with_title_hidden(settings.platform_specific.title_hidden)
.with_titlebar_transparent(
settings.platform_specific.titlebar_transparent,
)
.with_fullsize_content_view(
settings.platform_specific.fullsize_content_view,
);
}
#[cfg(target_os = "linux")]
{
#[cfg(feature = "x11")]
{
use winit::platform::x11::WindowBuilderExtX11;
window_builder = window_builder.with_name(
&settings.platform_specific.application_id,
&settings.platform_specific.application_id,
);
}
#[cfg(feature = "wayland")]
{
use winit::platform::wayland::WindowBuilderExtWayland;
window_builder = window_builder.with_name(
&settings.platform_specific.application_id,
&settings.platform_specific.application_id,
);
}
}
window_builder
}
/// Converts a winit window event into an iced event.
pub fn window_event(
id: window::Id,
@ -238,10 +348,9 @@ pub fn mode(mode: Option<winit::window::Fullscreen>) -> window::Mode {
}
}
/// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon.
/// Converts a [`mouse::Interaction`] to a [`winit`] cursor icon.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
pub fn mouse_interaction(
interaction: mouse::Interaction,
) -> winit::window::CursorIcon {
@ -263,10 +372,10 @@ pub fn mouse_interaction(
}
}
/// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button.
/// Converts a `MouseButton` from [`winit`] to an [`iced`] mouse button.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10
pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
match mouse_button {
winit::event::MouseButton::Left => mouse::Button::Left,
@ -276,11 +385,11 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
}
}
/// Converts some `ModifiersState` from [`winit`] to an [`iced_native`]
/// modifiers state.
/// Converts some `ModifiersState` from [`winit`] to an [`iced`] modifiers
/// state.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10
pub fn modifiers(
modifiers: winit::event::ModifiersState,
) -> keyboard::Modifiers {
@ -304,10 +413,10 @@ pub fn cursor_position(
Point::new(logical_position.x, logical_position.y)
}
/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event.
/// Converts a `Touch` from [`winit`] to an [`iced`] touch event.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10
pub fn touch_event(
touch: winit::event::Touch,
scale_factor: f64,
@ -335,10 +444,10 @@ pub fn touch_event(
}
}
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced`] key code.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
/// [`iced`]: https://github.com/iced-rs/iced/tree/0.10
pub fn key_code(
virtual_keycode: winit::event::VirtualKeyCode,
) -> keyboard::KeyCode {
@ -531,7 +640,7 @@ pub fn user_attention(
}
}
/// Converts some [`Icon`] into it's `winit` counterpart.
/// Converts some [`window::Icon`] into it's `winit` counterpart.
///
/// Returns `None` if there is an error during the conversion.
pub fn icon(icon: window::Icon) -> Option<winit::window::Icon> {

View file

@ -2,7 +2,7 @@
//!
//! ![The native path of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true)
//!
//! `iced_winit` offers some convenient abstractions on top of [`iced_native`]
//! `iced_winit` offers some convenient abstractions on top of [`iced_runtime`]
//! to quickstart development when using [`winit`].
//!
//! It exposes a renderer-agnostic [`Application`] trait that can be implemented
@ -11,25 +11,20 @@
//! Additionally, a [`conversion`] module is available for users that decide to
//! implement a custom event loop.
//!
//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.9/native
//! [`iced_runtime`]: https://github.com/iced-rs/iced/tree/0.10/runtime
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`conversion`]: crate::conversion
#![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
)]
#![forbid(rust_2018_idioms)]
#![deny(
missing_debug_implementations,
missing_docs,
unused_results,
clippy::extra_unused_lifetimes,
clippy::from_over_into,
clippy::needless_borrow,
clippy::new_without_default,
clippy::useless_conversion,
unsafe_code
unsafe_code,
rustdoc::broken_intra_doc_links
)]
#![forbid(rust_2018_idioms)]
#![allow(clippy::inherent_to_string, clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
pub use iced_graphics as graphics;
pub use iced_runtime as runtime;

View file

@ -4,6 +4,7 @@ mod windows;
pub use state::State;
use crate::conversion;
use crate::core::widget::operation;
use crate::core::{self, mouse, renderer, window, Size};
use crate::futures::futures::channel::mpsc;
@ -15,11 +16,9 @@ use crate::runtime::command::{self, Command};
use crate::runtime::multi_window::Program;
use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::Debug;
use crate::settings::window_builder;
use crate::style::application::StyleSheet;
use crate::{conversion, settings, Clipboard, Error, Proxy, Settings};
use crate::{Clipboard, Error, Proxy, Settings};
use iced_runtime::user_interface::Cache;
use std::mem::ManuallyDrop;
use std::time::Instant;
use winit::monitor::MonitorHandle;
@ -170,7 +169,7 @@ where
let should_main_be_visible = settings.window.visible;
let exit_on_close_request = settings.window.exit_on_close_request;
let builder = window_builder(
let builder = conversion::window_settings(
settings.window,
&application.title(window::Id::MAIN),
event_loop.primary_monitor(),
@ -270,10 +269,11 @@ where
}) => {
let exit_on_close_request = settings.exit_on_close_request;
let window =
settings::window_builder(settings, &title, monitor, None)
.build(window_target)
.expect("Failed to build window");
let window = conversion::window_settings(
settings, &title, monitor, None,
)
.build(window_target)
.expect("Failed to build window");
Some(winit::event::Event::UserEvent(Event::WindowCreated {
id,
@ -434,7 +434,7 @@ async fn run_instance<A, E, C>(
// TODO mw application update returns which window IDs to update
if !messages.is_empty() || uis_stale {
let mut cached_interfaces: Vec<Cache> =
let mut cached_interfaces: Vec<user_interface::Cache> =
ManuallyDrop::into_inner(user_interfaces)
.drain(..)
.map(UserInterface::into_cache)
@ -859,6 +859,9 @@ pub fn run_command<A, C, E>(
command::Action::Future(future) => {
runtime.spawn(Box::pin(future.map(Event::Application)));
}
command::Action::Stream(stream) => {
runtime.run(Box::pin(stream.map(Event::Application)));
}
command::Action::Clipboard(action) => match action {
clipboard::Action::Read(tag) => {
let message = tag(clipboard.read());
@ -1108,11 +1111,9 @@ pub fn user_force_quit(
event: &winit::event::WindowEvent<'_>,
_modifiers: winit::event::ModifiersState,
) -> bool {
use winit::event::WindowEvent;
match event {
#[cfg(target_os = "macos")]
WindowEvent::KeyboardInput {
winit::event::WindowEvent::KeyboardInput {
input:
winit::event::KeyboardInput {
virtual_keycode: Some(winit::event::VirtualKeyCode::Q),

View file

@ -1,9 +1,7 @@
//! Configure your application.
use crate::core::window;
use crate::conversion;
use winit::monitor::MonitorHandle;
use winit::window::WindowBuilder;
use std::borrow::Cow;
/// The settings of an application.
#[derive(Debug, Clone, Default)]
@ -21,87 +19,7 @@ pub struct Settings<Flags> {
///
/// [`Application`]: crate::Application
pub flags: Flags,
}
/// Converts the window settings into a `WindowBuilder` from `winit`.
pub fn window_builder(
settings: window::Settings,
title: &str,
monitor: Option<MonitorHandle>,
_id: Option<String>,
) -> WindowBuilder {
let mut window_builder = WindowBuilder::new();
let (width, height) = settings.size;
window_builder = window_builder
.with_title(title)
.with_inner_size(winit::dpi::LogicalSize { width, height })
.with_resizable(settings.resizable)
.with_decorations(settings.decorations)
.with_transparent(settings.transparent)
.with_window_icon(settings.icon.and_then(conversion::icon))
.with_window_level(conversion::window_level(settings.level))
.with_visible(settings.visible);
if let Some(position) =
conversion::position(monitor.as_ref(), settings.size, settings.position)
{
window_builder = window_builder.with_position(position);
}
if let Some((width, height)) = settings.min_size {
window_builder = window_builder
.with_min_inner_size(winit::dpi::LogicalSize { width, height });
}
if let Some((width, height)) = settings.max_size {
window_builder = window_builder
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
// `with_name` is available on both `WindowBuilderExtWayland` and `WindowBuilderExtX11` and they do
// exactly the same thing. We arbitrarily choose `WindowBuilderExtWayland` here.
use ::winit::platform::wayland::WindowBuilderExtWayland;
if let Some(id) = _id {
window_builder = window_builder.with_name(id.clone(), id);
}
}
#[cfg(target_os = "windows")]
{
use winit::platform::windows::WindowBuilderExtWindows;
#[allow(unsafe_code)]
unsafe {
window_builder = window_builder
.with_parent_window(settings.platform_specific.parent);
}
window_builder = window_builder
.with_drag_and_drop(settings.platform_specific.drag_and_drop);
}
#[cfg(target_os = "macos")]
{
use winit::platform::macos::WindowBuilderExtMacOS;
window_builder = window_builder
.with_title_hidden(settings.platform_specific.title_hidden)
.with_titlebar_transparent(
settings.platform_specific.titlebar_transparent,
)
.with_fullsize_content_view(
settings.platform_specific.fullsize_content_view,
);
}
window_builder
/// The fonts to load on boot.
pub fonts: Vec<Cow<'static, [u8]>>,
}

View file

@ -23,7 +23,7 @@ pub(crate) fn information(
let memory_used = sysinfo::get_current_pid()
.and_then(|pid| system.process(pid).ok_or("Process not found"))
.map(|process| process.memory())
.map(ProcessExt::memory)
.ok();
Information {