From 64e21535c7e5df9a1ff94b9b9036b6ae5b5c82b0 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 28 Jun 2022 14:27:06 -0300 Subject: Fix `multi_window` example --- examples/multi_window/src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/multi_window/src/main.rs (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs new file mode 100644 index 00000000..0ba6a591 --- /dev/null +++ b/examples/multi_window/src/main.rs @@ -0,0 +1,58 @@ +use iced::multi_window::Application; +use iced::pure::{button, column, text, Element}; +use iced::{window, Alignment, Command, Settings}; + +pub fn main() -> iced::Result { + Counter::run(Settings::default()) +} + +struct Counter { + value: i32, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + IncrementPressed, + DecrementPressed, +} + +impl Application for Counter { + type Flags = (); + type Executor = iced::executor::Default; + type Message = Message; + + fn new(_flags: ()) -> (Self, Command) { + (Self { value: 0 }, Command::none()) + } + + fn title(&self) -> String { + String::from("MultiWindow - Iced") + } + + fn windows(&self) -> Vec<(window::Id, iced::window::Settings)> { + todo!() + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::IncrementPressed => { + self.value += 1; + } + Message::DecrementPressed => { + self.value -= 1; + } + } + + Command::none() + } + + fn view(&self) -> Element { + column() + .padding(20) + .align_items(Alignment::Center) + .push(button("Increment").on_press(Message::IncrementPressed)) + .push(text(self.value.to_string()).size(50)) + .push(button("Decrement").on_press(Message::DecrementPressed)) + .into() + } +} -- cgit From 01bad4f89654d65b0d6a65a8df99c387cbadf7fe Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 14 Jul 2022 10:37:33 -0300 Subject: duplicate `pane_grid` example to `multi_window` --- examples/multi_window/Cargo.toml | 12 ++ examples/multi_window/src/main.rs | 370 +++++++++++++++++++++++++++++++++++--- 2 files changed, 355 insertions(+), 27 deletions(-) create mode 100644 examples/multi_window/Cargo.toml (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml new file mode 100644 index 00000000..9c3d0f21 --- /dev/null +++ b/examples/multi_window/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "multi_window" +version = "0.1.0" +authors = ["Richard Custodio "] +edition = "2021" +publish = false +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +iced = { path = "../..", features = ["debug", "multi_window"] } +iced_native = { path = "../../native" } +iced_lazy = { path = "../../lazy" } \ No newline at end of file diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 0ba6a591..ae8fa22b 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,58 +1,374 @@ -use iced::multi_window::Application; -use iced::pure::{button, column, text, Element}; -use iced::{window, Alignment, Command, Settings}; +use iced::alignment::{self, Alignment}; +use iced::executor; +use iced::keyboard; +use iced::theme::{self, Theme}; +use iced::widget::pane_grid::{self, PaneGrid}; +use iced::widget::{button, column, container, row, scrollable, text}; +use iced::{ + Application, Color, Command, Element, Length, Settings, Size, Subscription, +}; +use iced_lazy::responsive; +use iced_native::{event, subscription, Event}; pub fn main() -> iced::Result { - Counter::run(Settings::default()) + Example::run(Settings::default()) } -struct Counter { - value: i32, +struct Example { + panes: pane_grid::State, + panes_created: usize, + focus: Option, } #[derive(Debug, Clone, Copy)] enum Message { - IncrementPressed, - DecrementPressed, + Split(pane_grid::Axis, pane_grid::Pane), + SplitFocused(pane_grid::Axis), + FocusAdjacent(pane_grid::Direction), + Clicked(pane_grid::Pane), + Dragged(pane_grid::DragEvent), + Resized(pane_grid::ResizeEvent), + TogglePin(pane_grid::Pane), + Close(pane_grid::Pane), + CloseFocused, } -impl Application for Counter { - type Flags = (); - type Executor = iced::executor::Default; +impl Application for Example { type Message = Message; + type Theme = Theme; + type Executor = executor::Default; + type Flags = (); fn new(_flags: ()) -> (Self, Command) { - (Self { value: 0 }, Command::none()) - } + let (panes, _) = pane_grid::State::new(Pane::new(0)); - fn title(&self) -> String { - String::from("MultiWindow - Iced") + ( + Example { + panes, + panes_created: 1, + focus: None, + }, + Command::none(), + ) } - fn windows(&self) -> Vec<(window::Id, iced::window::Settings)> { - todo!() + fn title(&self) -> String { + String::from("Pane grid - Iced") } fn update(&mut self, message: Message) -> Command { match message { - Message::IncrementPressed => { - self.value += 1; + Message::Split(axis, pane) => { + let result = self.panes.split( + axis, + &pane, + Pane::new(self.panes_created), + ); + + if let Some((pane, _)) = result { + self.focus = Some(pane); + } + + self.panes_created += 1; + } + Message::SplitFocused(axis) => { + if let Some(pane) = self.focus { + let result = self.panes.split( + axis, + &pane, + Pane::new(self.panes_created), + ); + + if let Some((pane, _)) = result { + self.focus = Some(pane); + } + + self.panes_created += 1; + } + } + Message::FocusAdjacent(direction) => { + if let Some(pane) = self.focus { + if let Some(adjacent) = + self.panes.adjacent(&pane, direction) + { + self.focus = Some(adjacent); + } + } + } + Message::Clicked(pane) => { + self.focus = Some(pane); + } + Message::Resized(pane_grid::ResizeEvent { split, ratio }) => { + self.panes.resize(&split, ratio); + } + Message::Dragged(pane_grid::DragEvent::Dropped { + pane, + target, + }) => { + self.panes.swap(&pane, &target); } - Message::DecrementPressed => { - self.value -= 1; + Message::Dragged(_) => {} + Message::TogglePin(pane) => { + if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(&pane) + { + *is_pinned = !*is_pinned; + } + } + Message::Close(pane) => { + if let Some((_, sibling)) = self.panes.close(&pane) { + self.focus = Some(sibling); + } + } + Message::CloseFocused => { + if let Some(pane) = self.focus { + if let Some(Pane { is_pinned, .. }) = self.panes.get(&pane) + { + if !is_pinned { + if let Some((_, sibling)) = self.panes.close(&pane) + { + self.focus = Some(sibling); + } + } + } + } } } Command::none() } + fn subscription(&self) -> Subscription { + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + match event { + Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) if modifiers.command() => handle_hotkey(key_code), + _ => None, + } + }) + } + fn view(&self) -> Element { - column() - .padding(20) - .align_items(Alignment::Center) - .push(button("Increment").on_press(Message::IncrementPressed)) - .push(text(self.value.to_string()).size(50)) - .push(button("Decrement").on_press(Message::DecrementPressed)) + let focus = self.focus; + let total_panes = self.panes.len(); + + let pane_grid = PaneGrid::new(&self.panes, |id, pane| { + let is_focused = focus == Some(id); + + let pin_button = button( + text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), + ) + .on_press(Message::TogglePin(id)) + .padding(3); + + let title = row![ + pin_button, + "Pane", + text(pane.id.to_string()).style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }), + ] + .spacing(5); + + let title_bar = pane_grid::TitleBar::new(title) + .controls(view_controls(id, total_panes, pane.is_pinned)) + .padding(10) + .style(if is_focused { + style::title_bar_focused + } else { + style::title_bar_active + }); + + pane_grid::Content::new(responsive(move |size| { + view_content(id, total_panes, pane.is_pinned, size) + })) + .title_bar(title_bar) + .style(if is_focused { + style::pane_focused + } else { + style::pane_active + }) + }) + .width(Length::Fill) + .height(Length::Fill) + .spacing(10) + .on_click(Message::Clicked) + .on_drag(Message::Dragged) + .on_resize(10, Message::Resized); + + container(pane_grid) + .width(Length::Fill) + .height(Length::Fill) + .padding(10) .into() } } + +const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0xC7 as f32 / 255.0, + 0xC7 as f32 / 255.0, +); +const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0x47 as f32 / 255.0, + 0x47 as f32 / 255.0, +); + +fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { + use keyboard::KeyCode; + use pane_grid::{Axis, Direction}; + + let direction = match key_code { + KeyCode::Up => Some(Direction::Up), + KeyCode::Down => Some(Direction::Down), + KeyCode::Left => Some(Direction::Left), + KeyCode::Right => Some(Direction::Right), + _ => None, + }; + + match key_code { + KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)), + KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)), + KeyCode::W => Some(Message::CloseFocused), + _ => direction.map(Message::FocusAdjacent), + } +} + +struct Pane { + id: usize, + pub is_pinned: bool, +} + +impl Pane { + fn new(id: usize) -> Self { + Self { + id, + is_pinned: false, + } + } +} + +fn view_content<'a>( + pane: pane_grid::Pane, + total_panes: usize, + is_pinned: bool, + size: Size, +) -> Element<'a, Message> { + let button = |label, message| { + button( + text(label) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .size(16), + ) + .width(Length::Fill) + .padding(8) + .on_press(message) + }; + + let mut controls = column![ + button( + "Split horizontally", + Message::Split(pane_grid::Axis::Horizontal, pane), + ), + button( + "Split vertically", + Message::Split(pane_grid::Axis::Vertical, pane), + ) + ] + .spacing(5) + .max_width(150); + + if total_panes > 1 && !is_pinned { + controls = controls.push( + button("Close", Message::Close(pane)) + .style(theme::Button::Destructive), + ); + } + + let content = column![ + text(format!("{}x{}", size.width, size.height)).size(24), + controls, + ] + .width(Length::Fill) + .spacing(10) + .align_items(Alignment::Center); + + container(scrollable(content)) + .width(Length::Fill) + .height(Length::Fill) + .padding(5) + .center_y() + .into() +} + +fn view_controls<'a>( + pane: pane_grid::Pane, + total_panes: usize, + is_pinned: bool, +) -> Element<'a, Message> { + let mut button = button(text("Close").size(14)) + .style(theme::Button::Destructive) + .padding(3); + + if total_panes > 1 && !is_pinned { + button = button.on_press(Message::Close(pane)); + } + + button.into() +} + +mod style { + use iced::widget::container; + use iced::Theme; + + pub fn title_bar_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.background.strong.text), + background: Some(palette.background.strong.color.into()), + ..Default::default() + } + } + + pub fn title_bar_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.primary.strong.text), + background: Some(palette.primary.strong.color.into()), + ..Default::default() + } + } + + pub fn pane_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.background.strong.color, + ..Default::default() + } + } + + pub fn pane_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.primary.strong.color, + ..Default::default() + } + } +} -- cgit From 3d901d5f1f8e496651a6f9881fec92bc8998d910 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 21 Jul 2022 09:52:55 -0300 Subject: create multi-windowed `pane_grid` example --- examples/multi_window/src/main.rs | 379 ++++++++++++++++++++++++++++---------- 1 file changed, 278 insertions(+), 101 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index ae8fa22b..4ad92adb 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,36 +1,55 @@ use iced::alignment::{self, Alignment}; use iced::executor; use iced::keyboard; +use iced::multi_window::Application; use iced::theme::{self, Theme}; use iced::widget::pane_grid::{self, PaneGrid}; -use iced::widget::{button, column, container, row, scrollable, text}; -use iced::{ - Application, Color, Command, Element, Length, Settings, Size, Subscription, +use iced::widget::{ + button, column, container, pick_list, row, scrollable, text, text_input, }; +use iced::window; +use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; use iced_lazy::responsive; use iced_native::{event, subscription, Event}; +use std::collections::HashMap; + pub fn main() -> iced::Result { Example::run(Settings::default()) } struct Example { - panes: pane_grid::State, + windows: HashMap, panes_created: usize, + _focused: window::Id, +} + +struct Window { + title: String, + panes: pane_grid::State, focus: Option, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] enum Message { + Window(window::Id, WindowMessage), +} + +#[derive(Debug, Clone)] +enum WindowMessage { Split(pane_grid::Axis, pane_grid::Pane), SplitFocused(pane_grid::Axis), FocusAdjacent(pane_grid::Direction), Clicked(pane_grid::Pane), Dragged(pane_grid::DragEvent), + PopOut(pane_grid::Pane), Resized(pane_grid::ResizeEvent), + TitleChanged(String), + ToggleMoving(pane_grid::Pane), TogglePin(pane_grid::Pane), Close(pane_grid::Pane), CloseFocused, + SelectedWindow(pane_grid::Pane, SelectableWindow), } impl Application for Example { @@ -40,93 +59,158 @@ impl Application for Example { type Flags = (); fn new(_flags: ()) -> (Self, Command) { - let (panes, _) = pane_grid::State::new(Pane::new(0)); + let (panes, _) = + pane_grid::State::new(Pane::new(0, pane_grid::Axis::Horizontal)); + let window = Window { + panes, + focus: None, + title: String::from("Default window"), + }; ( Example { - panes, + windows: HashMap::from([(window::Id::new(0usize), window)]), panes_created: 1, - focus: None, + _focused: window::Id::new(0usize), }, Command::none(), ) } fn title(&self) -> String { - String::from("Pane grid - Iced") + String::from("Multi windowed pane grid - Iced") } fn update(&mut self, message: Message) -> Command { + let Message::Window(id, message) = message; match message { - Message::Split(axis, pane) => { - let result = self.panes.split( + WindowMessage::Split(axis, pane) => { + let window = self.windows.get_mut(&id).unwrap(); + let result = window.panes.split( axis, &pane, - Pane::new(self.panes_created), + Pane::new(self.panes_created, axis), ); if let Some((pane, _)) = result { - self.focus = Some(pane); + window.focus = Some(pane); } self.panes_created += 1; } - Message::SplitFocused(axis) => { - if let Some(pane) = self.focus { - let result = self.panes.split( + WindowMessage::SplitFocused(axis) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + let result = window.panes.split( axis, &pane, - Pane::new(self.panes_created), + Pane::new(self.panes_created, axis), ); if let Some((pane, _)) = result { - self.focus = Some(pane); + window.focus = Some(pane); } self.panes_created += 1; } } - Message::FocusAdjacent(direction) => { - if let Some(pane) = self.focus { + WindowMessage::FocusAdjacent(direction) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { if let Some(adjacent) = - self.panes.adjacent(&pane, direction) + window.panes.adjacent(&pane, direction) { - self.focus = Some(adjacent); + window.focus = Some(adjacent); } } } - Message::Clicked(pane) => { - self.focus = Some(pane); + WindowMessage::Clicked(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + window.focus = Some(pane); } - Message::Resized(pane_grid::ResizeEvent { split, ratio }) => { - self.panes.resize(&split, ratio); + WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.resize(&split, ratio); } - Message::Dragged(pane_grid::DragEvent::Dropped { + WindowMessage::SelectedWindow(pane, selected) => { + let window = self.windows.get_mut(&id).unwrap(); + let (mut pane, _) = window.panes.close(&pane).unwrap(); + pane.is_moving = false; + + if let Some(window) = self.windows.get_mut(&selected.0) { + let (&first_pane, _) = window.panes.iter().next().unwrap(); + let result = + window.panes.split(pane.axis, &first_pane, pane); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; + } + } + WindowMessage::ToggleMoving(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.panes.get_mut(&pane) { + pane.is_moving = !pane.is_moving; + } + } + WindowMessage::TitleChanged(title) => { + let window = self.windows.get_mut(&id).unwrap(); + window.title = title; + } + WindowMessage::PopOut(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((popped, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + + let (panes, _) = pane_grid::State::new(popped); + let window = Window { + panes, + focus: None, + title: format!("New window ({})", self.windows.len()), + }; + + self.windows + .insert(window::Id::new(self.windows.len()), window); + } + } + WindowMessage::Dragged(pane_grid::DragEvent::Dropped { pane, target, }) => { - self.panes.swap(&pane, &target); + let window = self.windows.get_mut(&id).unwrap(); + window.panes.swap(&pane, &target); } - Message::Dragged(_) => {} - Message::TogglePin(pane) => { - if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(&pane) + // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { + // println!("Picked {pane:?}"); + // } + WindowMessage::Dragged(_) => {} + WindowMessage::TogglePin(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(Pane { is_pinned, .. }) = + window.panes.get_mut(&pane) { *is_pinned = !*is_pinned; } } - Message::Close(pane) => { - if let Some((_, sibling)) = self.panes.close(&pane) { - self.focus = Some(sibling); + WindowMessage::Close(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((_, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); } } - Message::CloseFocused => { - if let Some(pane) = self.focus { - if let Some(Pane { is_pinned, .. }) = self.panes.get(&pane) + WindowMessage::CloseFocused => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(Pane { is_pinned, .. }) = + window.panes.get(&pane) { if !is_pinned { - if let Some((_, sibling)) = self.panes.close(&pane) + if let Some((_, sibling)) = + window.panes.close(&pane) { - self.focus = Some(sibling); + window.focus = Some(sibling); } } } @@ -147,66 +231,106 @@ impl Application for Example { Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - }) if modifiers.command() => handle_hotkey(key_code), + }) if modifiers.command() => { + handle_hotkey(key_code).map(|message| { + Message::Window(window::Id::new(0usize), message) + }) + } // TODO(derezzedex) _ => None, } }) } - fn view(&self) -> Element { - let focus = self.focus; - let total_panes = self.panes.len(); - - let pane_grid = PaneGrid::new(&self.panes, |id, pane| { - let is_focused = focus == Some(id); - - let pin_button = button( - text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), - ) - .on_press(Message::TogglePin(id)) - .padding(3); + fn windows(&self) -> Vec<(window::Id, iced::window::Settings)> { + self.windows + .iter() + .map(|(&id, _window)| (id, iced::window::Settings::default())) + .collect() + } - let title = row![ - pin_button, - "Pane", - text(pane.id.to_string()).style(if is_focused { - PANE_ID_COLOR_FOCUSED - } else { - PANE_ID_COLOR_UNFOCUSED - }), + fn view(&self, window_id: window::Id) -> Element { + if let Some(window) = self.windows.get(&window_id) { + let focus = window.focus; + let total_panes = window.panes.len(); + + let window_controls = row![ + text_input( + "Window title", + &window.title, + WindowMessage::TitleChanged, + ), + button(text("Apply")).style(theme::Button::Primary), ] - .spacing(5); - - let title_bar = pane_grid::TitleBar::new(title) - .controls(view_controls(id, total_panes, pane.is_pinned)) - .padding(10) + .spacing(5) + .align_items(Alignment::Center); + + let pane_grid = PaneGrid::new(&window.panes, |id, pane| { + let is_focused = focus == Some(id); + + let pin_button = button( + text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), + ) + .on_press(WindowMessage::TogglePin(id)) + .padding(3); + + let title = row![ + pin_button, + "Pane", + text(pane.id.to_string()).style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }), + ] + .spacing(5); + + let title_bar = pane_grid::TitleBar::new(title) + .controls(view_controls( + id, + total_panes, + pane.is_pinned, + pane.is_moving, + &window.title, + window_id, + &self.windows, + )) + .padding(10) + .style(if is_focused { + style::title_bar_focused + } else { + style::title_bar_active + }); + + pane_grid::Content::new(responsive(move |size| { + view_content(id, total_panes, pane.is_pinned, size) + })) + .title_bar(title_bar) .style(if is_focused { - style::title_bar_focused + style::pane_focused } else { - style::title_bar_active - }); - - pane_grid::Content::new(responsive(move |size| { - view_content(id, total_panes, pane.is_pinned, size) - })) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active + style::pane_active + }) }) - }) - .width(Length::Fill) - .height(Length::Fill) - .spacing(10) - .on_click(Message::Clicked) - .on_drag(Message::Dragged) - .on_resize(10, Message::Resized); - - container(pane_grid) .width(Length::Fill) .height(Length::Fill) - .padding(10) + .spacing(10) + .on_click(WindowMessage::Clicked) + .on_drag(WindowMessage::Dragged) + .on_resize(10, WindowMessage::Resized); + + let content: Element<_> = column![window_controls, pane_grid] + .width(Length::Fill) + .height(Length::Fill) + .padding(10) + .into(); + + return content + .map(move |message| Message::Window(window_id, message)); + } + + container(text("This shouldn't be possible!").size(20)) + .center_x() + .center_y() .into() } } @@ -222,7 +346,7 @@ const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( 0x47 as f32 / 255.0, ); -fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { +fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { use keyboard::KeyCode; use pane_grid::{Axis, Direction}; @@ -235,23 +359,44 @@ fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { }; match key_code { - KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)), - KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)), - KeyCode::W => Some(Message::CloseFocused), - _ => direction.map(Message::FocusAdjacent), + KeyCode::V => Some(WindowMessage::SplitFocused(Axis::Vertical)), + KeyCode::H => Some(WindowMessage::SplitFocused(Axis::Horizontal)), + KeyCode::W => Some(WindowMessage::CloseFocused), + _ => direction.map(WindowMessage::FocusAdjacent), + } +} + +#[derive(Debug, Clone)] +struct SelectableWindow(window::Id, String); + +impl PartialEq for SelectableWindow { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl Eq for SelectableWindow {} + +impl std::fmt::Display for SelectableWindow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.1.fmt(f) } } struct Pane { id: usize, + pub axis: pane_grid::Axis, pub is_pinned: bool, + pub is_moving: bool, } impl Pane { - fn new(id: usize) -> Self { + fn new(id: usize, axis: pane_grid::Axis) -> Self { Self { id, + axis, is_pinned: false, + is_moving: false, } } } @@ -261,7 +406,7 @@ fn view_content<'a>( total_panes: usize, is_pinned: bool, size: Size, -) -> Element<'a, Message> { +) -> Element<'a, WindowMessage> { let button = |label, message| { button( text(label) @@ -277,11 +422,11 @@ fn view_content<'a>( let mut controls = column![ button( "Split horizontally", - Message::Split(pane_grid::Axis::Horizontal, pane), + WindowMessage::Split(pane_grid::Axis::Horizontal, pane), ), button( "Split vertically", - Message::Split(pane_grid::Axis::Vertical, pane), + WindowMessage::Split(pane_grid::Axis::Vertical, pane), ) ] .spacing(5) @@ -289,7 +434,7 @@ fn view_content<'a>( if total_panes > 1 && !is_pinned { controls = controls.push( - button("Close", Message::Close(pane)) + button("Close", WindowMessage::Close(pane)) .style(theme::Button::Destructive), ); } @@ -314,16 +459,48 @@ fn view_controls<'a>( pane: pane_grid::Pane, total_panes: usize, is_pinned: bool, -) -> Element<'a, Message> { - let mut button = button(text("Close").size(14)) + is_moving: bool, + window_title: &'a str, + window_id: window::Id, + windows: &HashMap, +) -> Element<'a, WindowMessage> { + let window_selector = { + let options: Vec<_> = windows + .iter() + .map(|(id, window)| SelectableWindow(*id, window.title.clone())) + .collect(); + pick_list( + options, + Some(SelectableWindow(window_id, window_title.to_string())), + move |window| WindowMessage::SelectedWindow(pane, window), + ) + }; + + let mut move_to = button(text("Move to").size(14)).padding(3); + + let mut pop_out = button(text("Pop Out").size(14)).padding(3); + + let mut close = button(text("Close").size(14)) .style(theme::Button::Destructive) .padding(3); if total_panes > 1 && !is_pinned { - button = button.on_press(Message::Close(pane)); + close = close.on_press(WindowMessage::Close(pane)); + pop_out = pop_out.on_press(WindowMessage::PopOut(pane)); + } + + if windows.len() > 1 && total_panes > 1 && !is_pinned { + move_to = move_to.on_press(WindowMessage::ToggleMoving(pane)); + } + + let mut content = row![].spacing(10); + if is_moving { + content = content.push(pop_out).push(window_selector).push(close); + } else { + content = content.push(pop_out).push(move_to).push(close); } - button.into() + content.into() } mod style { -- cgit From 35331d0a41a53b8ff5c642b8274c7377ae6c6182 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 26 Jul 2022 16:46:12 -0300 Subject: Allow closing the window from user code --- examples/multi_window/src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 4ad92adb..ca137d48 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -50,6 +50,7 @@ enum WindowMessage { Close(pane_grid::Pane), CloseFocused, SelectedWindow(pane_grid::Pane, SelectableWindow), + CloseWindow, } impl Application for Example { @@ -128,6 +129,9 @@ impl Application for Example { let window = self.windows.get_mut(&id).unwrap(); window.focus = Some(pane); } + WindowMessage::CloseWindow => { + let _ = self.windows.remove(&id); + } WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { let window = self.windows.get_mut(&id).unwrap(); window.panes.resize(&split, ratio); @@ -145,8 +149,6 @@ impl Application for Example { if let Some((pane, _)) = result { window.focus = Some(pane); } - - self.panes_created += 1; } } WindowMessage::ToggleMoving(pane) => { @@ -260,6 +262,9 @@ impl Application for Example { WindowMessage::TitleChanged, ), button(text("Apply")).style(theme::Button::Primary), + button(text("Close")) + .on_press(WindowMessage::CloseWindow) + .style(theme::Button::Destructive), ] .spacing(5) .align_items(Alignment::Center); -- cgit From dc86bd03733969033df7389c3d21e78ecc6291bb Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 27 Jul 2022 15:37:48 -0300 Subject: Introduce `close_requested` for `multi-window` --- examples/multi_window/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index ca137d48..88ddf46f 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -250,6 +250,10 @@ impl Application for Example { .collect() } + fn close_requested(&self, window: window::Id) -> Self::Message { + Message::Window(window, WindowMessage::CloseWindow) + } + fn view(&self, window_id: window::Id) -> Element { if let Some(window) = self.windows.get(&window_id) { let focus = window.focus; -- cgit From 0ad53a3d5c7b5fb5785a64102ee1ad7df9a5fb2b Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 19 Sep 2022 20:59:37 -0300 Subject: add `window::Id` to `Event` and `Action` --- examples/events/src/main.rs | 2 +- examples/integration_opengl/src/main.rs | 2 ++ examples/integration_wgpu/src/main.rs | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 234e1423..e9709377 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -52,7 +52,7 @@ impl Application for Events { } } Message::EventOccurred(event) => { - if let Event::Window(window::Event::CloseRequested) = event { + if let Event::Window(_, window::Event::CloseRequested) = event { self.should_exit = true; } } diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index f161c8a0..56470190 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -13,6 +13,7 @@ use iced_glow::{Backend, Renderer, Settings, Viewport}; use iced_glutin::conversion; use iced_glutin::glutin; use iced_glutin::renderer; +use iced_glutin::window; use iced_glutin::{program, Clipboard, Color, Debug, Size}; pub fn main() { @@ -107,6 +108,7 @@ pub fn main() { // Map window event to iced event if let Some(event) = iced_winit::conversion::window_event( + window::Id::MAIN, &event, windowed_context.window().scale_factor(), modifiers, diff --git a/examples/integration_wgpu/src/main.rs b/examples/integration_wgpu/src/main.rs index 70f9a48b..219573ea 100644 --- a/examples/integration_wgpu/src/main.rs +++ b/examples/integration_wgpu/src/main.rs @@ -6,8 +6,8 @@ use scene::Scene; use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; use iced_winit::{ - conversion, futures, program, renderer, winit, Clipboard, Color, Debug, - Size, + conversion, futures, program, renderer, window, winit, Clipboard, Color, + Debug, Size, }; use winit::{ @@ -169,6 +169,7 @@ pub fn main() { // Map window event to iced event if let Some(event) = iced_winit::conversion::window_event( + window::Id::MAIN, &event, window.scale_factor(), modifiers, -- cgit From 064407635a0f9d79a067bad62f6f1042acaed18d Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 21 Sep 2022 19:17:25 -0300 Subject: implement `multi_window` for `iced_glutin` --- examples/multi_window/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 88ddf46f..0dda1804 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -70,9 +70,9 @@ impl Application for Example { ( Example { - windows: HashMap::from([(window::Id::new(0usize), window)]), + windows: HashMap::from([(window::Id::MAIN, window)]), panes_created: 1, - _focused: window::Id::new(0usize), + _focused: window::Id::MAIN, }, Command::none(), ) -- cgit From a386788b67bf4e008916e79a8c7dd7289a3ab3cd Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 28 Sep 2022 19:10:47 -0300 Subject: use `glutin/multi_window` branch --- examples/integration_opengl/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index 56470190..a9e9c732 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -31,7 +31,7 @@ pub fn main() { .unwrap(); unsafe { - let windowed_context = windowed_context.make_current().unwrap(); + let windowed_context = windowed_context.make_current(todo!("derezzedex")).unwrap(); let gl = glow::Context::from_loader_function(|s| { windowed_context.get_proc_address(s) as *const _ @@ -181,7 +181,7 @@ pub fn main() { ), ); - windowed_context.swap_buffers().unwrap(); + windowed_context.swap_buffers(todo!("derezzedex")).unwrap(); } _ => (), } -- cgit From 1bc0c480f9747826b244c30e92d8c4a29b576e4a Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 19 Oct 2022 22:56:00 -0300 Subject: move window settings to `iced_native` --- examples/integration_opengl/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index a9e9c732..fdbd7369 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -31,7 +31,8 @@ pub fn main() { .unwrap(); unsafe { - let windowed_context = windowed_context.make_current(todo!("derezzedex")).unwrap(); + let windowed_context = + windowed_context.make_current(todo!("derezzedex")).unwrap(); let gl = glow::Context::from_loader_function(|s| { windowed_context.get_proc_address(s) as *const _ -- cgit From 5e4e410b18eb744cf70ae1f18b9ef08611f59150 Mon Sep 17 00:00:00 2001 From: Richard Date: Thu, 3 Nov 2022 14:53:05 -0300 Subject: remove `windows` method (use commands instead) --- examples/multi_window/src/main.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 0dda1804..2771d728 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -131,6 +131,7 @@ impl Application for Example { } WindowMessage::CloseWindow => { let _ = self.windows.remove(&id); + return window::close(id); } WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { let window = self.windows.get_mut(&id).unwrap(); @@ -173,8 +174,9 @@ impl Application for Example { title: format!("New window ({})", self.windows.len()), }; - self.windows - .insert(window::Id::new(self.windows.len()), window); + let window_id = window::Id::new(self.windows.len()); + self.windows.insert(window_id, window); + return window::spawn(window_id, Default::default()); } } WindowMessage::Dragged(pane_grid::DragEvent::Dropped { @@ -243,13 +245,6 @@ impl Application for Example { }) } - fn windows(&self) -> Vec<(window::Id, iced::window::Settings)> { - self.windows - .iter() - .map(|(&id, _window)| (id, iced::window::Settings::default())) - .collect() - } - fn close_requested(&self, window: window::Id) -> Self::Message { Message::Window(window, WindowMessage::CloseWindow) } -- cgit From 942f1c91afb8257e289af8d0c229f74819f68361 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Mon, 2 Jan 2023 10:58:07 -0800 Subject: merged in iced master --- examples/multi_window/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 2771d728..9b93eea6 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -268,7 +268,7 @@ impl Application for Example { .spacing(5) .align_items(Alignment::Center); - let pane_grid = PaneGrid::new(&window.panes, |id, pane| { + let pane_grid = PaneGrid::new(&window.panes, |id, pane, _| { let is_focused = focus == Some(id); let pin_button = button( -- cgit From f43419d4752fe18065c0e1b7c2a26e65b9d6e253 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Mon, 2 Jan 2023 18:14:31 -0800 Subject: Fixed issue with window ID on winit --- examples/multi_window/Cargo.toml | 1 + examples/multi_window/src/main.rs | 2 ++ 2 files changed, 3 insertions(+) (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index 9c3d0f21..6de895d7 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -8,5 +8,6 @@ publish = false [dependencies] iced = { path = "../..", features = ["debug", "multi_window"] } +env_logger = "0.10.0" iced_native = { path = "../../native" } iced_lazy = { path = "../../lazy" } \ No newline at end of file diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 9b93eea6..9fe6b481 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -15,6 +15,8 @@ use iced_native::{event, subscription, Event}; use std::collections::HashMap; pub fn main() -> iced::Result { + env_logger::init(); + Example::run(Settings::default()) } -- cgit From ec41918ec40bddaba81235372f1566da59fd09f2 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Thu, 5 Jan 2023 15:26:28 -0800 Subject: Implemented window title update functionality for multiwindow. --- examples/multi_window/Cargo.toml | 2 +- examples/multi_window/src/main.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index 6de895d7..62198595 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -10,4 +10,4 @@ publish = false iced = { path = "../..", features = ["debug", "multi_window"] } env_logger = "0.10.0" iced_native = { path = "../../native" } -iced_lazy = { path = "../../lazy" } \ No newline at end of file +iced_lazy = { path = "../../lazy" } diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 9fe6b481..b9f0514c 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -26,6 +26,7 @@ struct Example { _focused: window::Id, } +#[derive(Debug)] struct Window { title: String, panes: pane_grid::State, @@ -80,8 +81,11 @@ impl Application for Example { ) } - fn title(&self) -> String { - String::from("Multi windowed pane grid - Iced") + fn title(&self, window: window::Id) -> String { + self.windows + .get(&window) + .map(|w| w.title.clone()) + .unwrap_or(String::from("New Window")) } fn update(&mut self, message: Message) -> Command { @@ -262,7 +266,6 @@ impl Application for Example { &window.title, WindowMessage::TitleChanged, ), - button(text("Apply")).style(theme::Button::Primary), button(text("Close")) .on_press(WindowMessage::CloseWindow) .style(theme::Button::Destructive), @@ -389,6 +392,7 @@ impl std::fmt::Display for SelectableWindow { } } +#[derive(Debug)] struct Pane { id: usize, pub axis: pane_grid::Axis, -- cgit From 3e5d34f25fa07fa99f57b686bbde87d73b8ed548 Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Mon, 9 Jan 2023 10:19:12 -0800 Subject: Formatting --- examples/multi_window/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index b9f0514c..18536bdf 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -57,9 +57,9 @@ enum WindowMessage { } impl Application for Example { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -251,10 +251,6 @@ impl Application for Example { }) } - fn close_requested(&self, window: window::Id) -> Self::Message { - Message::Window(window, WindowMessage::CloseWindow) - } - fn view(&self, window_id: window::Id) -> Element { if let Some(window) = self.windows.get(&window_id) { let focus = window.focus; @@ -342,6 +338,10 @@ impl Application for Example { .center_y() .into() } + + fn close_requested(&self, window: window::Id) -> Self::Message { + Message::Window(window, WindowMessage::CloseWindow) + } } const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( -- cgit From 0a643287deece9234b64cc843a9f6ae3e6e4806e Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 18 Jan 2023 17:04:11 -0800 Subject: Added window::Id to multi_window application's scale_factor --- examples/multi_window/src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 18536bdf..0d0a809b 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -12,6 +12,7 @@ use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; use iced_lazy::responsive; use iced_native::{event, subscription, Event}; +use iced_native::window::Id; use std::collections::HashMap; pub fn main() -> iced::Result { @@ -29,6 +30,7 @@ struct Example { #[derive(Debug)] struct Window { title: String, + scale: f64, panes: pane_grid::State, focus: Option, } @@ -69,6 +71,7 @@ impl Application for Example { panes, focus: None, title: String::from("Default window"), + scale: 1.0, }; ( @@ -178,6 +181,7 @@ impl Application for Example { panes, focus: None, title: format!("New window ({})", self.windows.len()), + scale: 1.0 + (self.windows.len() as f64 / 10.0), }; let window_id = window::Id::new(self.windows.len()); @@ -342,6 +346,10 @@ impl Application for Example { fn close_requested(&self, window: window::Id) -> Self::Message { Message::Window(window, WindowMessage::CloseWindow) } + + fn scale_factor(&self, window: Id) -> f64 { + self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) + } } const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( -- cgit From 367fea5dc8e94584334e880970126b40a046bfa6 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 15 Feb 2023 11:28:36 -0800 Subject: Redraw request events for multiwindow. --- examples/solar_system/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9a4ee754..eb461bb0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -89,7 +89,7 @@ impl Application for SolarSystem { } fn subscription(&self) -> Subscription { - window::frames().map(Message::Tick) + window::frames().map(|frame| Message::Tick(frame.at)) } } -- cgit From 64e0e817c27d720dc954ee94de58ded35b3f9f9a Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 15 Feb 2023 14:31:16 -0800 Subject: Widget operations for multi-window. --- examples/multi_window/src/main.rs | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 0d0a809b..23f08217 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -12,6 +12,7 @@ use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; use iced_lazy::responsive; use iced_native::{event, subscription, Event}; +use iced_native::widget::scrollable::{Properties, RelativeOffset}; use iced_native::window::Id; use std::collections::HashMap; @@ -56,6 +57,7 @@ enum WindowMessage { CloseFocused, SelectedWindow(pane_grid::Pane, SelectableWindow), CloseWindow, + SnapToggle, } impl Application for Example { @@ -94,6 +96,25 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { let Message::Window(id, message) = message; match message { + WindowMessage::SnapToggle => { + let window = self.windows.get_mut(&id).unwrap(); + + if let Some(focused) = &window.focus { + let pane = window.panes.get_mut(focused).unwrap(); + + let cmd = scrollable::snap_to( + pane.scrollable_id.clone(), + if pane.snapped { + RelativeOffset::START + } else { + RelativeOffset::END + }, + ); + + pane.snapped = !pane.snapped; + return cmd; + } + } WindowMessage::Split(axis, pane) => { let window = self.windows.get_mut(&id).unwrap(); let result = window.panes.split( @@ -311,7 +332,13 @@ impl Application for Example { }); pane_grid::Content::new(responsive(move |size| { - view_content(id, total_panes, pane.is_pinned, size) + view_content( + id, + pane.scrollable_id.clone(), + total_panes, + pane.is_pinned, + size, + ) })) .title_bar(title_bar) .style(if is_focused { @@ -403,24 +430,29 @@ impl std::fmt::Display for SelectableWindow { #[derive(Debug)] struct Pane { id: usize, + pub scrollable_id: scrollable::Id, pub axis: pane_grid::Axis, pub is_pinned: bool, pub is_moving: bool, + pub snapped: bool, } impl Pane { fn new(id: usize, axis: pane_grid::Axis) -> Self { Self { id, + scrollable_id: scrollable::Id::new(format!("{:?}", id)), axis, is_pinned: false, is_moving: false, + snapped: false, } } } fn view_content<'a>( pane: pane_grid::Pane, + scrollable_id: scrollable::Id, total_panes: usize, is_pinned: bool, size: Size, @@ -445,7 +477,8 @@ fn view_content<'a>( button( "Split vertically", WindowMessage::Split(pane_grid::Axis::Vertical, pane), - ) + ), + button("Snap", WindowMessage::SnapToggle,) ] .spacing(5) .max_width(150); @@ -462,15 +495,22 @@ fn view_content<'a>( controls, ] .width(Length::Fill) + .height(Length::Units(800)) .spacing(10) .align_items(Alignment::Center); - container(scrollable(content)) + Element::from( + container( + scrollable(content) + .vertical_scroll(Properties::new()) + .id(scrollable_id), + ) .width(Length::Fill) .height(Length::Fill) .padding(5) - .center_y() - .into() + .center_y(), + ) + .explain(Color::default()) } fn view_controls<'a>( -- cgit From 8da098330b58542cc929f4f24d02e26bd654bae4 Mon Sep 17 00:00:00 2001 From: Bingus Date: Fri, 17 Feb 2023 11:42:49 -0800 Subject: Fixed widget animations implementation --- examples/multi_window/src/main.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 23f08217..17d662b4 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -499,18 +499,17 @@ fn view_content<'a>( .spacing(10) .align_items(Alignment::Center); - Element::from( - container( - scrollable(content) - .vertical_scroll(Properties::new()) - .id(scrollable_id), - ) - .width(Length::Fill) - .height(Length::Fill) - .padding(5) - .center_y(), + container( + scrollable(content) + .height(Length::Fill) + .vertical_scroll(Properties::new()) + .id(scrollable_id), ) - .explain(Color::default()) + .width(Length::Fill) + .height(Length::Fill) + .padding(5) + .center_y() + .into() } fn view_controls<'a>( -- cgit From 2d427455ce8f9627da7c09eb80f38a615f0ddcb7 Mon Sep 17 00:00:00 2001 From: Bingus Date: Fri, 17 Feb 2023 11:50:52 -0800 Subject: Iced master merge (again) --- examples/multi_window/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 17d662b4..c2687ee6 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -441,7 +441,7 @@ impl Pane { fn new(id: usize, axis: pane_grid::Axis) -> Self { Self { id, - scrollable_id: scrollable::Id::new(format!("{:?}", id)), + scrollable_id: scrollable::Id::unique(), axis, is_pinned: false, is_moving: false, @@ -495,7 +495,7 @@ fn view_content<'a>( controls, ] .width(Length::Fill) - .height(Length::Units(800)) + .height(800) .spacing(10) .align_items(Alignment::Center); -- cgit From bd58d5fe25182908e99fdb0ced07b86666e45081 Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 20 Feb 2023 12:34:04 -0800 Subject: Cargo fix --- examples/multi_window/Cargo.toml | 2 +- examples/multi_window/src/main.rs | 304 ++++++++++++++++++++------------------ 2 files changed, 160 insertions(+), 146 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index 62198595..0bb83f37 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -7,7 +7,7 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -iced = { path = "../..", features = ["debug", "multi_window"] } +iced = { path = "../..", features = ["debug", "multi_window", "tokio"] } env_logger = "0.10.0" iced_native = { path = "../../native" } iced_lazy = { path = "../../lazy" } diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index c2687ee6..60f32a7d 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,5 +1,5 @@ use iced::alignment::{self, Alignment}; -use iced::executor; +use iced::{executor, time}; use iced::keyboard; use iced::multi_window::Application; use iced::theme::{self, Theme}; @@ -15,6 +15,7 @@ use iced_native::{event, subscription, Event}; use iced_native::widget::scrollable::{Properties, RelativeOffset}; use iced_native::window::Id; use std::collections::HashMap; +use std::time::{Duration, Instant}; pub fn main() -> iced::Result { env_logger::init(); @@ -25,6 +26,7 @@ pub fn main() -> iced::Result { struct Example { windows: HashMap, panes_created: usize, + count: usize, _focused: window::Id, } @@ -39,6 +41,7 @@ struct Window { #[derive(Debug, Clone)] enum Message { Window(window::Id, WindowMessage), + CountIncremented(Instant), } #[derive(Debug, Clone)] @@ -80,6 +83,7 @@ impl Application for Example { Example { windows: HashMap::from([(window::Id::MAIN, window)]), panes_created: 1, + count: 0, _focused: window::Id::MAIN, }, Command::none(), @@ -94,44 +98,29 @@ impl Application for Example { } fn update(&mut self, message: Message) -> Command { - let Message::Window(id, message) = message; match message { - WindowMessage::SnapToggle => { - let window = self.windows.get_mut(&id).unwrap(); - - if let Some(focused) = &window.focus { - let pane = window.panes.get_mut(focused).unwrap(); - - let cmd = scrollable::snap_to( - pane.scrollable_id.clone(), - if pane.snapped { - RelativeOffset::START - } else { - RelativeOffset::END - }, - ); - - pane.snapped = !pane.snapped; - return cmd; - } - } - WindowMessage::Split(axis, pane) => { - let window = self.windows.get_mut(&id).unwrap(); - let result = window.panes.split( - axis, - &pane, - Pane::new(self.panes_created, axis), - ); - - if let Some((pane, _)) = result { - window.focus = Some(pane); + Message::Window(id, message) => match message { + WindowMessage::SnapToggle => { + let window = self.windows.get_mut(&id).unwrap(); + + if let Some(focused) = &window.focus { + let pane = window.panes.get_mut(focused).unwrap(); + + let cmd = scrollable::snap_to( + pane.scrollable_id.clone(), + if pane.snapped { + RelativeOffset::START + } else { + RelativeOffset::END + }, + ); + + pane.snapped = !pane.snapped; + return cmd; + } } - - self.panes_created += 1; - } - WindowMessage::SplitFocused(axis) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { + WindowMessage::Split(axis, pane) => { + let window = self.windows.get_mut(&id).unwrap(); let result = window.panes.split( axis, &pane, @@ -144,112 +133,131 @@ impl Application for Example { self.panes_created += 1; } - } - WindowMessage::FocusAdjacent(direction) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - if let Some(adjacent) = - window.panes.adjacent(&pane, direction) - { - window.focus = Some(adjacent); + WindowMessage::SplitFocused(axis) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + let result = window.panes.split( + axis, + &pane, + Pane::new(self.panes_created, axis), + ); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; } } - } - WindowMessage::Clicked(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - window.focus = Some(pane); - } - WindowMessage::CloseWindow => { - let _ = self.windows.remove(&id); - return window::close(id); - } - WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.resize(&split, ratio); - } - WindowMessage::SelectedWindow(pane, selected) => { - let window = self.windows.get_mut(&id).unwrap(); - let (mut pane, _) = window.panes.close(&pane).unwrap(); - pane.is_moving = false; - - if let Some(window) = self.windows.get_mut(&selected.0) { - let (&first_pane, _) = window.panes.iter().next().unwrap(); - let result = - window.panes.split(pane.axis, &first_pane, pane); - - if let Some((pane, _)) = result { - window.focus = Some(pane); + WindowMessage::FocusAdjacent(direction) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(adjacent) = + window.panes.adjacent(&pane, direction) + { + window.focus = Some(adjacent); + } } } - } - WindowMessage::ToggleMoving(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.panes.get_mut(&pane) { - pane.is_moving = !pane.is_moving; + WindowMessage::Clicked(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + window.focus = Some(pane); } - } - WindowMessage::TitleChanged(title) => { - let window = self.windows.get_mut(&id).unwrap(); - window.title = title; - } - WindowMessage::PopOut(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((popped, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); - - let (panes, _) = pane_grid::State::new(popped); - let window = Window { - panes, - focus: None, - title: format!("New window ({})", self.windows.len()), - scale: 1.0 + (self.windows.len() as f64 / 10.0), - }; - - let window_id = window::Id::new(self.windows.len()); - self.windows.insert(window_id, window); - return window::spawn(window_id, Default::default()); + WindowMessage::CloseWindow => { + let _ = self.windows.remove(&id); + return window::close(id); } - } - WindowMessage::Dragged(pane_grid::DragEvent::Dropped { - pane, - target, - }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.swap(&pane, &target); - } - // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { - // println!("Picked {pane:?}"); - // } - WindowMessage::Dragged(_) => {} - WindowMessage::TogglePin(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(Pane { is_pinned, .. }) = - window.panes.get_mut(&pane) - { - *is_pinned = !*is_pinned; + WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.resize(&split, ratio); } - } - WindowMessage::Close(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((_, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); + WindowMessage::SelectedWindow(pane, selected) => { + let window = self.windows.get_mut(&id).unwrap(); + let (mut pane, _) = window.panes.close(&pane).unwrap(); + pane.is_moving = false; + + if let Some(window) = self.windows.get_mut(&selected.0) { + let (&first_pane, _) = window.panes.iter().next().unwrap(); + let result = + window.panes.split(pane.axis, &first_pane, pane); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + } } - } - WindowMessage::CloseFocused => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { + WindowMessage::ToggleMoving(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.panes.get_mut(&pane) { + pane.is_moving = !pane.is_moving; + } + } + WindowMessage::TitleChanged(title) => { + let window = self.windows.get_mut(&id).unwrap(); + window.title = title; + } + WindowMessage::PopOut(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((popped, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + + let (panes, _) = pane_grid::State::new(popped); + let window = Window { + panes, + focus: None, + title: format!("New window ({})", self.windows.len()), + scale: 1.0 + (self.windows.len() as f64 / 10.0), + }; + + let window_id = window::Id::new(self.windows.len()); + self.windows.insert(window_id, window); + return window::spawn(window_id, Default::default()); + } + } + WindowMessage::Dragged(pane_grid::DragEvent::Dropped { + pane, + target, + }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.swap(&pane, &target); + } + // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { + // println!("Picked {pane:?}"); + // } + WindowMessage::Dragged(_) => {} + WindowMessage::TogglePin(pane) => { + let window = self.windows.get_mut(&id).unwrap(); if let Some(Pane { is_pinned, .. }) = - window.panes.get(&pane) + window.panes.get_mut(&pane) { - if !is_pinned { - if let Some((_, sibling)) = - window.panes.close(&pane) - { - window.focus = Some(sibling); + *is_pinned = !*is_pinned; + } + } + WindowMessage::Close(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((_, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + } + } + WindowMessage::CloseFocused => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(Pane { is_pinned, .. }) = + window.panes.get(&pane) + { + if !is_pinned { + if let Some((_, sibling)) = + window.panes.close(&pane) + { + window.focus = Some(sibling); + } } } } } + }, + Message::CountIncremented(_) => { + self.count += 1; } } @@ -257,23 +265,26 @@ impl Application for Example { } fn subscription(&self) -> Subscription { - subscription::events_with(|event, status| { - if let event::Status::Captured = status { - return None; - } + Subscription::batch(vec![ + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } - match event { - Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) if modifiers.command() => { - handle_hotkey(key_code).map(|message| { - Message::Window(window::Id::new(0usize), message) - }) - } // TODO(derezzedex) - _ => None, - } - }) + match event { + Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) if modifiers.command() => { + handle_hotkey(key_code).map(|message| { + Message::Window(window::Id::new(0usize), message) + }) + } // TODO(derezzedex) + _ => None, + } + }), + time::every(Duration::from_secs(1)).map(Message::CountIncremented), + ]) } fn view(&self, window_id: window::Id) -> Element { @@ -335,6 +346,7 @@ impl Application for Example { view_content( id, pane.scrollable_id.clone(), + self.count, total_panes, pane.is_pinned, size, @@ -453,6 +465,7 @@ impl Pane { fn view_content<'a>( pane: pane_grid::Pane, scrollable_id: scrollable::Id, + count: usize, total_panes: usize, is_pinned: bool, size: Size, @@ -493,6 +506,7 @@ fn view_content<'a>( let content = column![ text(format!("{}x{}", size.width, size.height)).size(24), controls, + text(format!("{count}")).size(48), ] .width(Length::Fill) .height(800) -- cgit From e36daa6f937abd7cb2071fd8852a3c12263944ea Mon Sep 17 00:00:00 2001 From: bungoboingo Date: Tue, 28 Feb 2023 13:44:36 -0800 Subject: Removed glutin MW support and reverted glutin changes back to Iced master since it's being axed as we speak. --- examples/integration_opengl/src/main.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index fdbd7369..4dd3a4a9 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -13,7 +13,6 @@ use iced_glow::{Backend, Renderer, Settings, Viewport}; use iced_glutin::conversion; use iced_glutin::glutin; use iced_glutin::renderer; -use iced_glutin::window; use iced_glutin::{program, Clipboard, Color, Debug, Size}; pub fn main() { @@ -31,8 +30,7 @@ pub fn main() { .unwrap(); unsafe { - let windowed_context = - windowed_context.make_current(todo!("derezzedex")).unwrap(); + let windowed_context = windowed_context.make_current().unwrap(); let gl = glow::Context::from_loader_function(|s| { windowed_context.get_proc_address(s) as *const _ @@ -109,7 +107,7 @@ pub fn main() { // Map window event to iced event if let Some(event) = iced_winit::conversion::window_event( - window::Id::MAIN, + iced_winit::window::Id::MAIN, &event, windowed_context.window().scale_factor(), modifiers, @@ -182,7 +180,7 @@ pub fn main() { ), ); - windowed_context.swap_buffers(todo!("derezzedex")).unwrap(); + windowed_context.swap_buffers().unwrap(); } _ => (), } -- cgit From 8ba18430800142965549077373e2a45d0a3429a1 Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 13 Mar 2023 14:16:45 -0700 Subject: Code cleanup, clearer comments + removed some unnecessary dupe; Removed `Frames` struct return for `window::frames()` since we are just redrawing every window anyways; Interface dropping; --- examples/multi_window/Cargo.toml | 2 +- examples/solar_system/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index 0bb83f37..a59a0e68 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -7,7 +7,7 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -iced = { path = "../..", features = ["debug", "multi_window", "tokio"] } +iced = { path = "../..", features = ["debug", "multi-window", "tokio"] } env_logger = "0.10.0" iced_native = { path = "../../native" } iced_lazy = { path = "../../lazy" } diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index eb461bb0..9a4ee754 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -89,7 +89,7 @@ impl Application for SolarSystem { } fn subscription(&self) -> Subscription { - window::frames().map(|frame| Message::Tick(frame.at)) + window::frames().map(Message::Tick) } } -- cgit From ce4b2c93d9802dfb8cd3fc9033d76651d4bbc75b Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 13 Mar 2023 18:19:16 -0700 Subject: Added simpler MW example --- examples/multi_window/Cargo.toml | 8 +- examples/multi_window/src/main.rs | 654 +++++--------------------------- examples/multi_window_panes/Cargo.toml | 12 + examples/multi_window_panes/src/main.rs | 624 ++++++++++++++++++++++++++++++ 4 files changed, 739 insertions(+), 559 deletions(-) create mode 100644 examples/multi_window_panes/Cargo.toml create mode 100644 examples/multi_window_panes/src/main.rs (limited to 'examples') diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index a59a0e68..0cb5d546 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -1,13 +1,9 @@ [package] name = "multi_window" version = "0.1.0" -authors = ["Richard Custodio "] +authors = ["Bingus "] edition = "2021" publish = false -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -iced = { path = "../..", features = ["debug", "multi-window", "tokio"] } -env_logger = "0.10.0" -iced_native = { path = "../../native" } -iced_lazy = { path = "../../lazy" } +iced = { path = "../..", features = ["debug", "multi-window"] } \ No newline at end of file diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 60f32a7d..5699ece0 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,90 +1,50 @@ -use iced::alignment::{self, Alignment}; -use iced::{executor, time}; -use iced::keyboard; -use iced::multi_window::Application; -use iced::theme::{self, Theme}; -use iced::widget::pane_grid::{self, PaneGrid}; -use iced::widget::{ - button, column, container, pick_list, row, scrollable, text, text_input, +use iced::multi_window::{self, Application}; +use iced::widget::{button, column, container, scrollable, text, text_input}; +use iced::{ + executor, window, Alignment, Command, Element, Length, Settings, Theme, }; -use iced::window; -use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; -use iced_lazy::responsive; -use iced_native::{event, subscription, Event}; - -use iced_native::widget::scrollable::{Properties, RelativeOffset}; -use iced_native::window::Id; use std::collections::HashMap; -use std::time::{Duration, Instant}; - -pub fn main() -> iced::Result { - env_logger::init(); +fn main() -> iced::Result { Example::run(Settings::default()) } +#[derive(Default)] struct Example { + windows_count: usize, windows: HashMap, - panes_created: usize, - count: usize, - _focused: window::Id, } -#[derive(Debug)] struct Window { + id: window::Id, title: String, - scale: f64, - panes: pane_grid::State, - focus: Option, + scale_input: String, + current_scale: f64, } #[derive(Debug, Clone)] enum Message { - Window(window::Id, WindowMessage), - CountIncremented(Instant), -} - -#[derive(Debug, Clone)] -enum WindowMessage { - Split(pane_grid::Axis, pane_grid::Pane), - SplitFocused(pane_grid::Axis), - FocusAdjacent(pane_grid::Direction), - Clicked(pane_grid::Pane), - Dragged(pane_grid::DragEvent), - PopOut(pane_grid::Pane), - Resized(pane_grid::ResizeEvent), - TitleChanged(String), - ToggleMoving(pane_grid::Pane), - TogglePin(pane_grid::Pane), - Close(pane_grid::Pane), - CloseFocused, - SelectedWindow(pane_grid::Pane, SelectableWindow), - CloseWindow, - SnapToggle, + ScaleInputChanged(window::Id, String), + ScaleChanged(window::Id, String), + TitleChanged(window::Id, String), + CloseWindow(window::Id), + NewWindow, } -impl Application for Example { +impl multi_window::Application for Example { type Executor = executor::Default; type Message = Message; type Theme = Theme; type Flags = (); fn new(_flags: ()) -> (Self, Command) { - let (panes, _) = - pane_grid::State::new(Pane::new(0, pane_grid::Axis::Horizontal)); - let window = Window { - panes, - focus: None, - title: String::from("Default window"), - scale: 1.0, - }; - ( Example { - windows: HashMap::from([(window::Id::MAIN, window)]), - panes_created: 1, - count: 0, - _focused: window::Id::MAIN, + windows_count: 0, + windows: HashMap::from([( + window::Id::MAIN, + Window::new(window::Id::MAIN), + )]), }, Command::none(), ) @@ -93,530 +53,118 @@ impl Application for Example { fn title(&self, window: window::Id) -> String { self.windows .get(&window) - .map(|w| w.title.clone()) - .unwrap_or(String::from("New Window")) + .map(|window| window.title.clone()) + .unwrap_or("Example".to_string()) } fn update(&mut self, message: Message) -> Command { match message { - Message::Window(id, message) => match message { - WindowMessage::SnapToggle => { - let window = self.windows.get_mut(&id).unwrap(); - - if let Some(focused) = &window.focus { - let pane = window.panes.get_mut(focused).unwrap(); - - let cmd = scrollable::snap_to( - pane.scrollable_id.clone(), - if pane.snapped { - RelativeOffset::START - } else { - RelativeOffset::END - }, - ); - - pane.snapped = !pane.snapped; - return cmd; - } - } - WindowMessage::Split(axis, pane) => { - let window = self.windows.get_mut(&id).unwrap(); - let result = window.panes.split( - axis, - &pane, - Pane::new(self.panes_created, axis), - ); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - - self.panes_created += 1; - } - WindowMessage::SplitFocused(axis) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - let result = window.panes.split( - axis, - &pane, - Pane::new(self.panes_created, axis), - ); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - - self.panes_created += 1; - } - } - WindowMessage::FocusAdjacent(direction) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - if let Some(adjacent) = - window.panes.adjacent(&pane, direction) - { - window.focus = Some(adjacent); - } - } - } - WindowMessage::Clicked(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - window.focus = Some(pane); - } - WindowMessage::CloseWindow => { - let _ = self.windows.remove(&id); - return window::close(id); - } - WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.resize(&split, ratio); - } - WindowMessage::SelectedWindow(pane, selected) => { - let window = self.windows.get_mut(&id).unwrap(); - let (mut pane, _) = window.panes.close(&pane).unwrap(); - pane.is_moving = false; - - if let Some(window) = self.windows.get_mut(&selected.0) { - let (&first_pane, _) = window.panes.iter().next().unwrap(); - let result = - window.panes.split(pane.axis, &first_pane, pane); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - } - } - WindowMessage::ToggleMoving(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.panes.get_mut(&pane) { - pane.is_moving = !pane.is_moving; - } - } - WindowMessage::TitleChanged(title) => { - let window = self.windows.get_mut(&id).unwrap(); - window.title = title; - } - WindowMessage::PopOut(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((popped, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); + Message::ScaleInputChanged(id, scale) => { + let window = + self.windows.get_mut(&id).expect("Window not found!"); + window.scale_input = scale; + } + Message::ScaleChanged(id, scale) => { + let window = + self.windows.get_mut(&id).expect("Window not found!"); + + window.current_scale = scale + .parse::() + .unwrap_or(window.current_scale) + .clamp(0.5, 5.0); + } + Message::TitleChanged(id, title) => { + let window = + self.windows.get_mut(&id).expect("Window not found."); - let (panes, _) = pane_grid::State::new(popped); - let window = Window { - panes, - focus: None, - title: format!("New window ({})", self.windows.len()), - scale: 1.0 + (self.windows.len() as f64 / 10.0), - }; + window.title = title; + } + Message::CloseWindow(id) => { + return window::close(id); + } + Message::NewWindow => { + self.windows_count += 1; + let id = window::Id::new(self.windows_count); + self.windows.insert(id, Window::new(id)); - let window_id = window::Id::new(self.windows.len()); - self.windows.insert(window_id, window); - return window::spawn(window_id, Default::default()); - } - } - WindowMessage::Dragged(pane_grid::DragEvent::Dropped { - pane, - target, - }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.swap(&pane, &target); - } - // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { - // println!("Picked {pane:?}"); - // } - WindowMessage::Dragged(_) => {} - WindowMessage::TogglePin(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(Pane { is_pinned, .. }) = - window.panes.get_mut(&pane) - { - *is_pinned = !*is_pinned; - } - } - WindowMessage::Close(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((_, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); - } - } - WindowMessage::CloseFocused => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - if let Some(Pane { is_pinned, .. }) = - window.panes.get(&pane) - { - if !is_pinned { - if let Some((_, sibling)) = - window.panes.close(&pane) - { - window.focus = Some(sibling); - } - } - } - } - } - }, - Message::CountIncremented(_) => { - self.count += 1; + return window::spawn(id, window::Settings::default()); } } Command::none() } - fn subscription(&self) -> Subscription { - Subscription::batch(vec![ - subscription::events_with(|event, status| { - if let event::Status::Captured = status { - return None; - } - - match event { - Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) if modifiers.command() => { - handle_hotkey(key_code).map(|message| { - Message::Window(window::Id::new(0usize), message) - }) - } // TODO(derezzedex) - _ => None, - } - }), - time::every(Duration::from_secs(1)).map(Message::CountIncremented), - ]) - } - - fn view(&self, window_id: window::Id) -> Element { - if let Some(window) = self.windows.get(&window_id) { - let focus = window.focus; - let total_panes = window.panes.len(); - - let window_controls = row![ - text_input( - "Window title", - &window.title, - WindowMessage::TitleChanged, - ), - button(text("Close")) - .on_press(WindowMessage::CloseWindow) - .style(theme::Button::Destructive), - ] - .spacing(5) - .align_items(Alignment::Center); - - let pane_grid = PaneGrid::new(&window.panes, |id, pane, _| { - let is_focused = focus == Some(id); - - let pin_button = button( - text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), - ) - .on_press(WindowMessage::TogglePin(id)) - .padding(3); - - let title = row![ - pin_button, - "Pane", - text(pane.id.to_string()).style(if is_focused { - PANE_ID_COLOR_FOCUSED - } else { - PANE_ID_COLOR_UNFOCUSED - }), - ] - .spacing(5); - - let title_bar = pane_grid::TitleBar::new(title) - .controls(view_controls( - id, - total_panes, - pane.is_pinned, - pane.is_moving, - &window.title, - window_id, - &self.windows, - )) - .padding(10) - .style(if is_focused { - style::title_bar_focused - } else { - style::title_bar_active - }); + fn view(&self, window: window::Id) -> Element { + let window = self + .windows + .get(&window) + .map(|window| window.view()) + .unwrap(); - pane_grid::Content::new(responsive(move |size| { - view_content( - id, - pane.scrollable_id.clone(), - self.count, - total_panes, - pane.is_pinned, - size, - ) - })) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active - }) - }) + container(window) .width(Length::Fill) .height(Length::Fill) - .spacing(10) - .on_click(WindowMessage::Clicked) - .on_drag(WindowMessage::Dragged) - .on_resize(10, WindowMessage::Resized); - - let content: Element<_> = column![window_controls, pane_grid] - .width(Length::Fill) - .height(Length::Fill) - .padding(10) - .into(); - - return content - .map(move |message| Message::Window(window_id, message)); - } - - container(text("This shouldn't be possible!").size(20)) .center_x() .center_y() .into() } - fn close_requested(&self, window: window::Id) -> Self::Message { - Message::Window(window, WindowMessage::CloseWindow) - } - - fn scale_factor(&self, window: Id) -> f64 { - self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) - } -} - -const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( - 0xFF as f32 / 255.0, - 0xC7 as f32 / 255.0, - 0xC7 as f32 / 255.0, -); -const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( - 0xFF as f32 / 255.0, - 0x47 as f32 / 255.0, - 0x47 as f32 / 255.0, -); - -fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { - use keyboard::KeyCode; - use pane_grid::{Axis, Direction}; - - let direction = match key_code { - KeyCode::Up => Some(Direction::Up), - KeyCode::Down => Some(Direction::Down), - KeyCode::Left => Some(Direction::Left), - KeyCode::Right => Some(Direction::Right), - _ => None, - }; - - match key_code { - KeyCode::V => Some(WindowMessage::SplitFocused(Axis::Vertical)), - KeyCode::H => Some(WindowMessage::SplitFocused(Axis::Horizontal)), - KeyCode::W => Some(WindowMessage::CloseFocused), - _ => direction.map(WindowMessage::FocusAdjacent), - } -} - -#[derive(Debug, Clone)] -struct SelectableWindow(window::Id, String); - -impl PartialEq for SelectableWindow { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 + fn scale_factor(&self, window: window::Id) -> f64 { + self.windows + .get(&window) + .map(|window| window.current_scale) + .unwrap_or(1.0) } -} - -impl Eq for SelectableWindow {} -impl std::fmt::Display for SelectableWindow { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.1.fmt(f) + fn close_requested(&self, window: window::Id) -> Self::Message { + Message::CloseWindow(window) } } -#[derive(Debug)] -struct Pane { - id: usize, - pub scrollable_id: scrollable::Id, - pub axis: pane_grid::Axis, - pub is_pinned: bool, - pub is_moving: bool, - pub snapped: bool, -} - -impl Pane { - fn new(id: usize, axis: pane_grid::Axis) -> Self { +impl Window { + fn new(id: window::Id) -> Self { Self { id, - scrollable_id: scrollable::Id::unique(), - axis, - is_pinned: false, - is_moving: false, - snapped: false, + title: "Window".to_string(), + scale_input: "1.0".to_string(), + current_scale: 1.0, } } -} - -fn view_content<'a>( - pane: pane_grid::Pane, - scrollable_id: scrollable::Id, - count: usize, - total_panes: usize, - is_pinned: bool, - size: Size, -) -> Element<'a, WindowMessage> { - let button = |label, message| { - button( - text(label) - .width(Length::Fill) - .horizontal_alignment(alignment::Horizontal::Center) - .size(16), - ) - .width(Length::Fill) - .padding(8) - .on_press(message) - }; - let mut controls = column![ - button( - "Split horizontally", - WindowMessage::Split(pane_grid::Axis::Horizontal, pane), - ), - button( - "Split vertically", - WindowMessage::Split(pane_grid::Axis::Vertical, pane), - ), - button("Snap", WindowMessage::SnapToggle,) - ] - .spacing(5) - .max_width(150); - - if total_panes > 1 && !is_pinned { - controls = controls.push( - button("Close", WindowMessage::Close(pane)) - .style(theme::Button::Destructive), - ); + fn view(&self) -> Element { + window_view(self.id, &self.scale_input, &self.title) } - - let content = column![ - text(format!("{}x{}", size.width, size.height)).size(24), - controls, - text(format!("{count}")).size(48), - ] - .width(Length::Fill) - .height(800) - .spacing(10) - .align_items(Alignment::Center); - - container( - scrollable(content) - .height(Length::Fill) - .vertical_scroll(Properties::new()) - .id(scrollable_id), - ) - .width(Length::Fill) - .height(Length::Fill) - .padding(5) - .center_y() - .into() } -fn view_controls<'a>( - pane: pane_grid::Pane, - total_panes: usize, - is_pinned: bool, - is_moving: bool, - window_title: &'a str, - window_id: window::Id, - windows: &HashMap, -) -> Element<'a, WindowMessage> { - let window_selector = { - let options: Vec<_> = windows - .iter() - .map(|(id, window)| SelectableWindow(*id, window.title.clone())) - .collect(); - pick_list( - options, - Some(SelectableWindow(window_id, window_title.to_string())), - move |window| WindowMessage::SelectedWindow(pane, window), - ) - }; - - let mut move_to = button(text("Move to").size(14)).padding(3); - - let mut pop_out = button(text("Pop Out").size(14)).padding(3); - - let mut close = button(text("Close").size(14)) - .style(theme::Button::Destructive) - .padding(3); - - if total_panes > 1 && !is_pinned { - close = close.on_press(WindowMessage::Close(pane)); - pop_out = pop_out.on_press(WindowMessage::PopOut(pane)); - } - - if windows.len() > 1 && total_panes > 1 && !is_pinned { - move_to = move_to.on_press(WindowMessage::ToggleMoving(pane)); - } - - let mut content = row![].spacing(10); - if is_moving { - content = content.push(pop_out).push(window_selector).push(close); - } else { - content = content.push(pop_out).push(move_to).push(close); - } - - content.into() -} - -mod style { - use iced::widget::container; - use iced::Theme; - - pub fn title_bar_active(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - text_color: Some(palette.background.strong.text), - background: Some(palette.background.strong.color.into()), - ..Default::default() - } - } - - pub fn title_bar_focused(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - text_color: Some(palette.primary.strong.text), - background: Some(palette.primary.strong.color.into()), - ..Default::default() - } - } - - pub fn pane_active(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - background: Some(palette.background.weak.color.into()), - border_width: 2.0, - border_color: palette.background.strong.color, - ..Default::default() - } - } - - pub fn pane_focused(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); +fn window_view<'a>( + id: window::Id, + scale_input: &'a str, + title: &'a str, +) -> Element<'a, Message> { + let scale_input = column![ + text("Window scale factor:"), + text_input("Window Scale", scale_input, move |msg| { + Message::ScaleInputChanged(id, msg) + }) + .on_submit(Message::ScaleChanged(id, scale_input.to_string())) + ]; + + let title_input = column![ + text("Window title:"), + text_input("Window Title", title, move |msg| { + Message::TitleChanged(id, msg) + }) + ]; + + let new_window_button = + button(text("New Window")).on_press(Message::NewWindow); + + let content = scrollable( + column![scale_input, title_input, new_window_button] + .spacing(50) + .width(Length::Fill) + .align_items(Alignment::Center), + ); - container::Appearance { - background: Some(palette.background.weak.color.into()), - border_width: 2.0, - border_color: palette.primary.strong.color, - ..Default::default() - } - } + container(content).width(200).center_x().into() } diff --git a/examples/multi_window_panes/Cargo.toml b/examples/multi_window_panes/Cargo.toml new file mode 100644 index 00000000..1b3f3ec6 --- /dev/null +++ b/examples/multi_window_panes/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "multi_window_panes" +version = "0.1.0" +authors = ["Richard Custodio "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug", "multi-window", "tokio"] } +env_logger = "0.10.0" +iced_native = { path = "../../native" } +iced_lazy = { path = "../../lazy" } diff --git a/examples/multi_window_panes/src/main.rs b/examples/multi_window_panes/src/main.rs new file mode 100644 index 00000000..b8b63769 --- /dev/null +++ b/examples/multi_window_panes/src/main.rs @@ -0,0 +1,624 @@ +use iced::alignment::{self, Alignment}; +use iced::{executor, time}; +use iced::keyboard; +use iced::multi_window::Application; +use iced::theme::{self, Theme}; +use iced::widget::pane_grid::{self, PaneGrid}; +use iced::widget::{ + button, column, container, pick_list, row, scrollable, text, text_input, +}; +use iced::window; +use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; +use iced_lazy::responsive; +use iced_native::{event, subscription, Event}; + +use iced_native::widget::scrollable::{Properties, RelativeOffset}; +use iced_native::window::Id; +use std::collections::HashMap; +use std::time::{Duration, Instant}; + +pub fn main() -> iced::Result { + env_logger::init(); + + Example::run(Settings::default()) +} + +struct Example { + windows: HashMap, + panes_created: usize, + count: usize, + _focused: window::Id, +} + +#[derive(Debug)] +struct Window { + title: String, + scale: f64, + panes: pane_grid::State, + focus: Option, +} + +#[derive(Debug, Clone)] +enum Message { + Window(window::Id, WindowMessage), + CountIncremented(Instant), +} + +#[derive(Debug, Clone)] +enum WindowMessage { + Split(pane_grid::Axis, pane_grid::Pane), + SplitFocused(pane_grid::Axis), + FocusAdjacent(pane_grid::Direction), + Clicked(pane_grid::Pane), + Dragged(pane_grid::DragEvent), + PopOut(pane_grid::Pane), + Resized(pane_grid::ResizeEvent), + TitleChanged(String), + ToggleMoving(pane_grid::Pane), + TogglePin(pane_grid::Pane), + Close(pane_grid::Pane), + CloseFocused, + SelectedWindow(pane_grid::Pane, SelectableWindow), + CloseWindow, + SnapToggle, +} + +impl Application for Example { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: ()) -> (Self, Command) { + let (panes, _) = + pane_grid::State::new(Pane::new(0, pane_grid::Axis::Horizontal)); + let window = Window { + panes, + focus: None, + title: String::from("Default window"), + scale: 1.0, + }; + + ( + Example { + windows: HashMap::from([(window::Id::MAIN, window)]), + panes_created: 1, + count: 0, + _focused: window::Id::MAIN, + }, + Command::none(), + ) + } + + fn title(&self, window: window::Id) -> String { + self.windows + .get(&window) + .map(|w| w.title.clone()) + .unwrap_or(String::from("New Window")) + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::Window(id, message) => match message { + WindowMessage::SnapToggle => { + let window = self.windows.get_mut(&id).unwrap(); + + if let Some(focused) = &window.focus { + let pane = window.panes.get_mut(focused).unwrap(); + + let cmd = scrollable::snap_to( + pane.scrollable_id.clone(), + if pane.snapped { + RelativeOffset::START + } else { + RelativeOffset::END + }, + ); + + pane.snapped = !pane.snapped; + return cmd; + } + } + WindowMessage::Split(axis, pane) => { + let window = self.windows.get_mut(&id).unwrap(); + let result = window.panes.split( + axis, + &pane, + Pane::new(self.panes_created, axis), + ); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; + } + WindowMessage::SplitFocused(axis) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + let result = window.panes.split( + axis, + &pane, + Pane::new(self.panes_created, axis), + ); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + + self.panes_created += 1; + } + } + WindowMessage::FocusAdjacent(direction) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(adjacent) = + window.panes.adjacent(&pane, direction) + { + window.focus = Some(adjacent); + } + } + } + WindowMessage::Clicked(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + window.focus = Some(pane); + } + WindowMessage::CloseWindow => { + let _ = self.windows.remove(&id); + return window::close(id); + } + WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.resize(&split, ratio); + } + WindowMessage::SelectedWindow(pane, selected) => { + let window = self.windows.get_mut(&id).unwrap(); + let (mut pane, _) = window.panes.close(&pane).unwrap(); + pane.is_moving = false; + + if let Some(window) = self.windows.get_mut(&selected.0) { + let (&first_pane, _) = window.panes.iter().next().unwrap(); + let result = + window.panes.split(pane.axis, &first_pane, pane); + + if let Some((pane, _)) = result { + window.focus = Some(pane); + } + } + } + WindowMessage::ToggleMoving(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.panes.get_mut(&pane) { + pane.is_moving = !pane.is_moving; + } + } + WindowMessage::TitleChanged(title) => { + let window = self.windows.get_mut(&id).unwrap(); + window.title = title; + } + WindowMessage::PopOut(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((popped, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + + let (panes, _) = pane_grid::State::new(popped); + let window = Window { + panes, + focus: None, + title: format!("New window ({})", self.windows.len()), + scale: 1.0 + (self.windows.len() as f64 / 10.0), + }; + + let window_id = window::Id::new(self.windows.len()); + self.windows.insert(window_id, window); + return window::spawn(window_id, Default::default()); + } + } + WindowMessage::Dragged(pane_grid::DragEvent::Dropped { + pane, + target, + }) => { + let window = self.windows.get_mut(&id).unwrap(); + window.panes.swap(&pane, &target); + } + // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { + // println!("Picked {pane:?}"); + // } + WindowMessage::Dragged(_) => {} + WindowMessage::TogglePin(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(Pane { is_pinned, .. }) = + window.panes.get_mut(&pane) + { + *is_pinned = !*is_pinned; + } + } + WindowMessage::Close(pane) => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some((_, sibling)) = window.panes.close(&pane) { + window.focus = Some(sibling); + } + } + WindowMessage::CloseFocused => { + let window = self.windows.get_mut(&id).unwrap(); + if let Some(pane) = window.focus { + if let Some(Pane { is_pinned, .. }) = + window.panes.get(&pane) + { + if !is_pinned { + if let Some((_, sibling)) = + window.panes.close(&pane) + { + window.focus = Some(sibling); + } + } + } + } + } + }, + Message::CountIncremented(_) => { + self.count += 1; + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription { + Subscription::batch(vec![ + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + match event { + Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) if modifiers.command() => { + handle_hotkey(key_code).map(|message| { + Message::Window(window::Id::new(0usize), message) + }) + } // TODO(derezzedex) + _ => None, + } + }), + time::every(Duration::from_secs(1)).map(Message::CountIncremented), + ]) + } + + fn view(&self, window: window::Id) -> Element { + let window_id = window; + + if let Some(window) = self.windows.get(&window) { + let focus = window.focus; + let total_panes = window.panes.len(); + + let window_controls = row![ + text_input( + "Window title", + &window.title, + WindowMessage::TitleChanged, + ), + button(text("Close")) + .on_press(WindowMessage::CloseWindow) + .style(theme::Button::Destructive), + ] + .spacing(5) + .align_items(Alignment::Center); + + let pane_grid = PaneGrid::new(&window.panes, |id, pane, _| { + let is_focused = focus == Some(id); + + let pin_button = button( + text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), + ) + .on_press(WindowMessage::TogglePin(id)) + .padding(3); + + let title = row![ + pin_button, + "Pane", + text(pane.id.to_string()).style(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }), + ] + .spacing(5); + + let title_bar = pane_grid::TitleBar::new(title) + .controls(view_controls( + id, + total_panes, + pane.is_pinned, + pane.is_moving, + &window.title, + window_id, + &self.windows, + )) + .padding(10) + .style(if is_focused { + style::title_bar_focused + } else { + style::title_bar_active + }); + + pane_grid::Content::new(responsive(move |size| { + view_content( + id, + pane.scrollable_id.clone(), + self.count, + total_panes, + pane.is_pinned, + size, + ) + })) + .title_bar(title_bar) + .style(if is_focused { + style::pane_focused + } else { + style::pane_active + }) + }) + .width(Length::Fill) + .height(Length::Fill) + .spacing(10) + .on_click(WindowMessage::Clicked) + .on_drag(WindowMessage::Dragged) + .on_resize(10, WindowMessage::Resized); + + let content: Element<_> = column![window_controls, pane_grid] + .width(Length::Fill) + .height(Length::Fill) + .padding(10) + .into(); + + return content + .map(move |message| Message::Window(window_id, message)); + } + + container(text("This shouldn't be possible!").size(20)) + .center_x() + .center_y() + .into() + } + + fn close_requested(&self, window: window::Id) -> Self::Message { + Message::Window(window, WindowMessage::CloseWindow) + } + + fn scale_factor(&self, window: Id) -> f64 { + self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) + } +} + +const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0xC7 as f32 / 255.0, + 0xC7 as f32 / 255.0, +); +const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0x47 as f32 / 255.0, + 0x47 as f32 / 255.0, +); + +fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { + use keyboard::KeyCode; + use pane_grid::{Axis, Direction}; + + let direction = match key_code { + KeyCode::Up => Some(Direction::Up), + KeyCode::Down => Some(Direction::Down), + KeyCode::Left => Some(Direction::Left), + KeyCode::Right => Some(Direction::Right), + _ => None, + }; + + match key_code { + KeyCode::V => Some(WindowMessage::SplitFocused(Axis::Vertical)), + KeyCode::H => Some(WindowMessage::SplitFocused(Axis::Horizontal)), + KeyCode::W => Some(WindowMessage::CloseFocused), + _ => direction.map(WindowMessage::FocusAdjacent), + } +} + +#[derive(Debug, Clone)] +struct SelectableWindow(window::Id, String); + +impl PartialEq for SelectableWindow { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl Eq for SelectableWindow {} + +impl std::fmt::Display for SelectableWindow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.1.fmt(f) + } +} + +#[derive(Debug)] +struct Pane { + id: usize, + pub scrollable_id: scrollable::Id, + pub axis: pane_grid::Axis, + pub is_pinned: bool, + pub is_moving: bool, + pub snapped: bool, +} + +impl Pane { + fn new(id: usize, axis: pane_grid::Axis) -> Self { + Self { + id, + scrollable_id: scrollable::Id::unique(), + axis, + is_pinned: false, + is_moving: false, + snapped: false, + } + } +} + +fn view_content<'a>( + pane: pane_grid::Pane, + scrollable_id: scrollable::Id, + count: usize, + total_panes: usize, + is_pinned: bool, + size: Size, +) -> Element<'a, WindowMessage> { + let button = |label, message| { + button( + text(label) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .size(16), + ) + .width(Length::Fill) + .padding(8) + .on_press(message) + }; + + let mut controls = column![ + button( + "Split horizontally", + WindowMessage::Split(pane_grid::Axis::Horizontal, pane), + ), + button( + "Split vertically", + WindowMessage::Split(pane_grid::Axis::Vertical, pane), + ), + button("Snap", WindowMessage::SnapToggle,) + ] + .spacing(5) + .max_width(150); + + if total_panes > 1 && !is_pinned { + controls = controls.push( + button("Close", WindowMessage::Close(pane)) + .style(theme::Button::Destructive), + ); + } + + let content = column![ + text(format!("{}x{}", size.width, size.height)).size(24), + controls, + text(format!("{count}")).size(48), + ] + .width(Length::Fill) + .height(800) + .spacing(10) + .align_items(Alignment::Center); + + container( + scrollable(content) + .height(Length::Fill) + .vertical_scroll(Properties::new()) + .id(scrollable_id), + ) + .width(Length::Fill) + .height(Length::Fill) + .padding(5) + .center_y() + .into() +} + +fn view_controls<'a>( + pane: pane_grid::Pane, + total_panes: usize, + is_pinned: bool, + is_moving: bool, + window_title: &'a str, + window_id: window::Id, + windows: &HashMap, +) -> Element<'a, WindowMessage> { + let window_selector = { + let options: Vec<_> = windows + .iter() + .map(|(id, window)| SelectableWindow(*id, window.title.clone())) + .collect(); + pick_list( + options, + Some(SelectableWindow(window_id, window_title.to_string())), + move |window| WindowMessage::SelectedWindow(pane, window), + ) + }; + + let mut move_to = button(text("Move to").size(14)).padding(3); + + let mut pop_out = button(text("Pop Out").size(14)).padding(3); + + let mut close = button(text("Close").size(14)) + .style(theme::Button::Destructive) + .padding(3); + + if total_panes > 1 && !is_pinned { + close = close.on_press(WindowMessage::Close(pane)); + pop_out = pop_out.on_press(WindowMessage::PopOut(pane)); + } + + if windows.len() > 1 && total_panes > 1 && !is_pinned { + move_to = move_to.on_press(WindowMessage::ToggleMoving(pane)); + } + + let mut content = row![].spacing(10); + if is_moving { + content = content.push(pop_out).push(window_selector).push(close); + } else { + content = content.push(pop_out).push(move_to).push(close); + } + + content.into() +} + +mod style { + use iced::widget::container; + use iced::Theme; + + pub fn title_bar_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.background.strong.text), + background: Some(palette.background.strong.color.into()), + ..Default::default() + } + } + + pub fn title_bar_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + text_color: Some(palette.primary.strong.text), + background: Some(palette.primary.strong.color.into()), + ..Default::default() + } + } + + pub fn pane_active(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.background.strong.color, + ..Default::default() + } + } + + pub fn pane_focused(theme: &Theme) -> container::Appearance { + let palette = theme.extended_palette(); + + container::Appearance { + background: Some(palette.background.weak.color.into()), + border_width: 2.0, + border_color: palette.primary.strong.color, + ..Default::default() + } + } +} -- cgit From 41836dd80d0534608e7aedfbf2319c540a23de1a Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 15 Mar 2023 18:20:38 -0700 Subject: Added per-window theme support. --- examples/multi_window_panes/src/main.rs | 41 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/multi_window_panes/src/main.rs b/examples/multi_window_panes/src/main.rs index b8b63769..b1d0a3bc 100644 --- a/examples/multi_window_panes/src/main.rs +++ b/examples/multi_window_panes/src/main.rs @@ -1,5 +1,4 @@ use iced::alignment::{self, Alignment}; -use iced::{executor, time}; use iced::keyboard; use iced::multi_window::Application; use iced::theme::{self, Theme}; @@ -8,6 +7,7 @@ use iced::widget::{ button, column, container, pick_list, row, scrollable, text, text_input, }; use iced::window; +use iced::{executor, time}; use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; use iced_lazy::responsive; use iced_native::{event, subscription, Event}; @@ -34,6 +34,7 @@ struct Example { struct Window { title: String, scale: f64, + theme: Theme, panes: pane_grid::State, focus: Option, } @@ -77,6 +78,7 @@ impl Application for Example { focus: None, title: String::from("Default window"), scale: 1.0, + theme: Theme::default(), }; ( @@ -167,7 +169,10 @@ impl Application for Example { let _ = self.windows.remove(&id); return window::close(id); } - WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { + WindowMessage::Resized(pane_grid::ResizeEvent { + split, + ratio, + }) => { let window = self.windows.get_mut(&id).unwrap(); window.panes.resize(&split, ratio); } @@ -177,7 +182,8 @@ impl Application for Example { pane.is_moving = false; if let Some(window) = self.windows.get_mut(&selected.0) { - let (&first_pane, _) = window.panes.iter().next().unwrap(); + let (&first_pane, _) = + window.panes.iter().next().unwrap(); let result = window.panes.split(pane.axis, &first_pane, pane); @@ -205,8 +211,16 @@ impl Application for Example { let window = Window { panes, focus: None, - title: format!("New window ({})", self.windows.len()), + title: format!( + "New window ({})", + self.windows.len() + ), scale: 1.0 + (self.windows.len() as f64 / 10.0), + theme: if self.windows.len() % 2 == 0 { + Theme::Light + } else { + Theme::Dark + }, }; let window_id = window::Id::new(self.windows.len()); @@ -215,15 +229,12 @@ impl Application for Example { } } WindowMessage::Dragged(pane_grid::DragEvent::Dropped { - pane, - target, - }) => { + pane, + target, + }) => { let window = self.windows.get_mut(&id).unwrap(); window.panes.swap(&pane, &target); } - // WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => { - // println!("Picked {pane:?}"); - // } WindowMessage::Dragged(_) => {} WindowMessage::TogglePin(pane) => { let window = self.windows.get_mut(&id).unwrap(); @@ -273,9 +284,9 @@ impl Application for Example { match event { Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) if modifiers.command() => { + modifiers, + key_code, + }) if modifiers.command() => { handle_hotkey(key_code).map(|message| { Message::Window(window::Id::new(0usize), message) }) @@ -391,6 +402,10 @@ impl Application for Example { fn scale_factor(&self, window: Id) -> f64 { self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) } + + fn theme(&self, window: Id) -> Theme { + self.windows.get(&window).expect("Window not found!").theme.clone() + } } const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( -- cgit From d53ccc857da4d4cda769904342aeb5a82a64f146 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 12 Jul 2023 19:21:05 -0700 Subject: refactored window storage; new helper window events (Destroyed, Created); clippy + fmt; --- examples/events/src/main.rs | 7 +- examples/exit/src/main.rs | 2 +- examples/integration/src/main.rs | 2 +- examples/integration_opengl/src/main.rs | 0 examples/loading_spinners/src/circular.rs | 2 +- examples/loading_spinners/src/linear.rs | 2 +- examples/multi_window/Cargo.toml | 2 +- examples/multi_window/src/main.rs | 162 +++++--- examples/multi_window_panes/Cargo.toml | 12 - examples/multi_window_panes/src/main.rs | 639 ------------------------------ examples/screenshot/src/main.rs | 7 +- examples/toast/src/main.rs | 4 +- examples/todos/src/main.rs | 2 +- 13 files changed, 120 insertions(+), 723 deletions(-) delete mode 100644 examples/integration_opengl/src/main.rs delete mode 100644 examples/multi_window_panes/Cargo.toml delete mode 100644 examples/multi_window_panes/src/main.rs (limited to 'examples') diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 70659f52..c3ac6fd1 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -26,7 +26,7 @@ struct Events { enum Message { EventOccurred(Event), Toggled(bool), - Exit(window::Id), + Exit, } impl Application for Events { @@ -55,7 +55,8 @@ impl Application for Events { Command::none() } Message::EventOccurred(event) => { - if let Event::Window(id, window::Event::CloseRequested) = event { + if let Event::Window(id, window::Event::CloseRequested) = event + { window::close(id) } else { Command::none() @@ -66,7 +67,7 @@ impl Application for Events { Command::none() } - Message::Exit(id) => window::close(id), + Message::Exit => window::close(window::Id::MAIN), } } diff --git a/examples/exit/src/main.rs b/examples/exit/src/main.rs index 6152f627..ec618dc1 100644 --- a/examples/exit/src/main.rs +++ b/examples/exit/src/main.rs @@ -34,7 +34,7 @@ impl Application for Exit { fn update(&mut self, message: Message) -> Command { match message { - Message::Confirm => window::close(), + Message::Confirm => window::close(window::Id::MAIN), Message::Exit => { self.show_confirm = true; diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index a560959a..90beb097 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -6,8 +6,8 @@ use scene::Scene; use iced_wgpu::graphics::Viewport; use iced_wgpu::{wgpu, Backend, Renderer, Settings}; -use iced_winit::core::mouse; use iced_winit::core::renderer; +use iced_winit::core::{mouse, window}; use iced_winit::core::{Color, Size}; use iced_winit::runtime::program; use iced_winit::runtime::Debug; diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs index 3a35e029..ff599231 100644 --- a/examples/loading_spinners/src/circular.rs +++ b/examples/loading_spinners/src/circular.rs @@ -277,7 +277,7 @@ where let state = tree.state.downcast_mut::(); - if let Event::Window(window::Event::RedrawRequested(now)) = event { + if let Event::Window(_, window::Event::RedrawRequested(now)) = event { state.animation = state.animation.timed_transition( self.cycle_duration, self.rotation_duration, diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs index 3d95729b..8e07c12b 100644 --- a/examples/loading_spinners/src/linear.rs +++ b/examples/loading_spinners/src/linear.rs @@ -198,7 +198,7 @@ where let state = tree.state.downcast_mut::(); - if let Event::Window(window::Event::RedrawRequested(now)) = event { + if let Event::Window(_, window::Event::RedrawRequested(now)) = event { *state = state.timed_transition(self.cycle_duration, now); shell.request_redraw(RedrawRequest::At( diff --git a/examples/multi_window/Cargo.toml b/examples/multi_window/Cargo.toml index 0cb5d546..2e222dfb 100644 --- a/examples/multi_window/Cargo.toml +++ b/examples/multi_window/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../..", features = ["debug", "multi-window"] } \ No newline at end of file +iced = { path = "../..", features = ["debug", "multi-window"] } diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 5699ece0..58604702 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,25 +1,32 @@ use iced::multi_window::{self, Application}; use iced::widget::{button, column, container, scrollable, text, text_input}; +use iced::window::{Id, Position}; use iced::{ - executor, window, Alignment, Command, Element, Length, Settings, Theme, + executor, subscription, window, Alignment, Command, Element, Length, + Settings, Subscription, Theme, }; use std::collections::HashMap; fn main() -> iced::Result { - Example::run(Settings::default()) + Example::run(Settings { + exit_on_close_request: false, + ..Default::default() + }) } #[derive(Default)] struct Example { - windows_count: usize, windows: HashMap, + next_window_pos: window::Position, } +#[derive(Debug)] struct Window { - id: window::Id, title: String, scale_input: String, current_scale: f64, + theme: Theme, + input_id: iced::widget::text_input::Id, } #[derive(Debug, Clone)] @@ -28,6 +35,8 @@ enum Message { ScaleChanged(window::Id, String), TitleChanged(window::Id, String), CloseWindow(window::Id), + WindowDestroyed(window::Id), + WindowCreated(window::Id, (i32, i32)), NewWindow, } @@ -40,11 +49,8 @@ impl multi_window::Application for Example { fn new(_flags: ()) -> (Self, Command) { ( Example { - windows_count: 0, - windows: HashMap::from([( - window::Id::MAIN, - Window::new(window::Id::MAIN), - )]), + windows: HashMap::from([(window::Id::MAIN, Window::new(1))]), + next_window_pos: Position::Default, }, Command::none(), ) @@ -82,12 +88,32 @@ impl multi_window::Application for Example { Message::CloseWindow(id) => { return window::close(id); } + Message::WindowDestroyed(id) => { + self.windows.remove(&id); + } + Message::WindowCreated(id, position) => { + self.next_window_pos = window::Position::Specific( + position.0 + 20, + position.1 + 20, + ); + + if let Some(window) = self.windows.get(&id) { + return text_input::focus(window.input_id.clone()); + } + } Message::NewWindow => { - self.windows_count += 1; - let id = window::Id::new(self.windows_count); - self.windows.insert(id, Window::new(id)); - - return window::spawn(id, window::Settings::default()); + let count = self.windows.len() + 1; + let id = window::Id::new(count); + + self.windows.insert(id, Window::new(count)); + + return window::spawn( + id, + window::Settings { + position: self.next_window_pos, + ..Default::default() + }, + ); } } @@ -95,13 +121,9 @@ impl multi_window::Application for Example { } fn view(&self, window: window::Id) -> Element { - let window = self - .windows - .get(&window) - .map(|window| window.view()) - .unwrap(); + let content = self.windows.get(&window).unwrap().view(window); - container(window) + container(content) .width(Length::Fill) .height(Length::Fill) .center_x() @@ -109,6 +131,10 @@ impl multi_window::Application for Example { .into() } + fn theme(&self, window: Id) -> Self::Theme { + self.windows.get(&window).unwrap().theme.clone() + } + fn scale_factor(&self, window: window::Id) -> f64 { self.windows .get(&window) @@ -116,55 +142,71 @@ impl multi_window::Application for Example { .unwrap_or(1.0) } - fn close_requested(&self, window: window::Id) -> Self::Message { - Message::CloseWindow(window) + fn subscription(&self) -> Subscription { + subscription::events_with(|event, _| { + if let iced::Event::Window(id, window_event) = event { + match window_event { + window::Event::CloseRequested => { + Some(Message::CloseWindow(id)) + } + window::Event::Destroyed => { + Some(Message::WindowDestroyed(id)) + } + window::Event::Created { position, .. } => { + Some(Message::WindowCreated(id, position)) + } + _ => None, + } + } else { + None + } + }) } } impl Window { - fn new(id: window::Id) -> Self { + fn new(count: usize) -> Self { Self { - id, - title: "Window".to_string(), + title: format!("Window_{}", count), scale_input: "1.0".to_string(), current_scale: 1.0, + theme: if count % 2 == 0 { + Theme::Light + } else { + Theme::Dark + }, + input_id: text_input::Id::unique(), } } - fn view(&self) -> Element { - window_view(self.id, &self.scale_input, &self.title) + fn view(&self, id: window::Id) -> Element { + let scale_input = column![ + text("Window scale factor:"), + text_input("Window Scale", &self.scale_input) + .on_input(move |msg| { Message::ScaleInputChanged(id, msg) }) + .on_submit(Message::ScaleChanged( + id, + self.scale_input.to_string() + )) + ]; + + let title_input = column![ + text("Window title:"), + text_input("Window Title", &self.title) + .on_input(move |msg| { Message::TitleChanged(id, msg) }) + .id(self.input_id.clone()) + ]; + + let new_window_button = + button(text("New Window")).on_press(Message::NewWindow); + + let content = scrollable( + column![scale_input, title_input, new_window_button] + .spacing(50) + .width(Length::Fill) + .align_items(Alignment::Center), + ); + + container(content).width(200).center_x().into() } } - -fn window_view<'a>( - id: window::Id, - scale_input: &'a str, - title: &'a str, -) -> Element<'a, Message> { - let scale_input = column![ - text("Window scale factor:"), - text_input("Window Scale", scale_input, move |msg| { - Message::ScaleInputChanged(id, msg) - }) - .on_submit(Message::ScaleChanged(id, scale_input.to_string())) - ]; - - let title_input = column![ - text("Window title:"), - text_input("Window Title", title, move |msg| { - Message::TitleChanged(id, msg) - }) - ]; - - let new_window_button = - button(text("New Window")).on_press(Message::NewWindow); - - let content = scrollable( - column![scale_input, title_input, new_window_button] - .spacing(50) - .width(Length::Fill) - .align_items(Alignment::Center), - ); - - container(content).width(200).center_x().into() -} diff --git a/examples/multi_window_panes/Cargo.toml b/examples/multi_window_panes/Cargo.toml deleted file mode 100644 index 1b3f3ec6..00000000 --- a/examples/multi_window_panes/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "multi_window_panes" -version = "0.1.0" -authors = ["Richard Custodio "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../..", features = ["debug", "multi-window", "tokio"] } -env_logger = "0.10.0" -iced_native = { path = "../../native" } -iced_lazy = { path = "../../lazy" } diff --git a/examples/multi_window_panes/src/main.rs b/examples/multi_window_panes/src/main.rs deleted file mode 100644 index b1d0a3bc..00000000 --- a/examples/multi_window_panes/src/main.rs +++ /dev/null @@ -1,639 +0,0 @@ -use iced::alignment::{self, Alignment}; -use iced::keyboard; -use iced::multi_window::Application; -use iced::theme::{self, Theme}; -use iced::widget::pane_grid::{self, PaneGrid}; -use iced::widget::{ - button, column, container, pick_list, row, scrollable, text, text_input, -}; -use iced::window; -use iced::{executor, time}; -use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; -use iced_lazy::responsive; -use iced_native::{event, subscription, Event}; - -use iced_native::widget::scrollable::{Properties, RelativeOffset}; -use iced_native::window::Id; -use std::collections::HashMap; -use std::time::{Duration, Instant}; - -pub fn main() -> iced::Result { - env_logger::init(); - - Example::run(Settings::default()) -} - -struct Example { - windows: HashMap, - panes_created: usize, - count: usize, - _focused: window::Id, -} - -#[derive(Debug)] -struct Window { - title: String, - scale: f64, - theme: Theme, - panes: pane_grid::State, - focus: Option, -} - -#[derive(Debug, Clone)] -enum Message { - Window(window::Id, WindowMessage), - CountIncremented(Instant), -} - -#[derive(Debug, Clone)] -enum WindowMessage { - Split(pane_grid::Axis, pane_grid::Pane), - SplitFocused(pane_grid::Axis), - FocusAdjacent(pane_grid::Direction), - Clicked(pane_grid::Pane), - Dragged(pane_grid::DragEvent), - PopOut(pane_grid::Pane), - Resized(pane_grid::ResizeEvent), - TitleChanged(String), - ToggleMoving(pane_grid::Pane), - TogglePin(pane_grid::Pane), - Close(pane_grid::Pane), - CloseFocused, - SelectedWindow(pane_grid::Pane, SelectableWindow), - CloseWindow, - SnapToggle, -} - -impl Application for Example { - type Executor = executor::Default; - type Message = Message; - type Theme = Theme; - type Flags = (); - - fn new(_flags: ()) -> (Self, Command) { - let (panes, _) = - pane_grid::State::new(Pane::new(0, pane_grid::Axis::Horizontal)); - let window = Window { - panes, - focus: None, - title: String::from("Default window"), - scale: 1.0, - theme: Theme::default(), - }; - - ( - Example { - windows: HashMap::from([(window::Id::MAIN, window)]), - panes_created: 1, - count: 0, - _focused: window::Id::MAIN, - }, - Command::none(), - ) - } - - fn title(&self, window: window::Id) -> String { - self.windows - .get(&window) - .map(|w| w.title.clone()) - .unwrap_or(String::from("New Window")) - } - - fn update(&mut self, message: Message) -> Command { - match message { - Message::Window(id, message) => match message { - WindowMessage::SnapToggle => { - let window = self.windows.get_mut(&id).unwrap(); - - if let Some(focused) = &window.focus { - let pane = window.panes.get_mut(focused).unwrap(); - - let cmd = scrollable::snap_to( - pane.scrollable_id.clone(), - if pane.snapped { - RelativeOffset::START - } else { - RelativeOffset::END - }, - ); - - pane.snapped = !pane.snapped; - return cmd; - } - } - WindowMessage::Split(axis, pane) => { - let window = self.windows.get_mut(&id).unwrap(); - let result = window.panes.split( - axis, - &pane, - Pane::new(self.panes_created, axis), - ); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - - self.panes_created += 1; - } - WindowMessage::SplitFocused(axis) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - let result = window.panes.split( - axis, - &pane, - Pane::new(self.panes_created, axis), - ); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - - self.panes_created += 1; - } - } - WindowMessage::FocusAdjacent(direction) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - if let Some(adjacent) = - window.panes.adjacent(&pane, direction) - { - window.focus = Some(adjacent); - } - } - } - WindowMessage::Clicked(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - window.focus = Some(pane); - } - WindowMessage::CloseWindow => { - let _ = self.windows.remove(&id); - return window::close(id); - } - WindowMessage::Resized(pane_grid::ResizeEvent { - split, - ratio, - }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.resize(&split, ratio); - } - WindowMessage::SelectedWindow(pane, selected) => { - let window = self.windows.get_mut(&id).unwrap(); - let (mut pane, _) = window.panes.close(&pane).unwrap(); - pane.is_moving = false; - - if let Some(window) = self.windows.get_mut(&selected.0) { - let (&first_pane, _) = - window.panes.iter().next().unwrap(); - let result = - window.panes.split(pane.axis, &first_pane, pane); - - if let Some((pane, _)) = result { - window.focus = Some(pane); - } - } - } - WindowMessage::ToggleMoving(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.panes.get_mut(&pane) { - pane.is_moving = !pane.is_moving; - } - } - WindowMessage::TitleChanged(title) => { - let window = self.windows.get_mut(&id).unwrap(); - window.title = title; - } - WindowMessage::PopOut(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((popped, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); - - let (panes, _) = pane_grid::State::new(popped); - let window = Window { - panes, - focus: None, - title: format!( - "New window ({})", - self.windows.len() - ), - scale: 1.0 + (self.windows.len() as f64 / 10.0), - theme: if self.windows.len() % 2 == 0 { - Theme::Light - } else { - Theme::Dark - }, - }; - - let window_id = window::Id::new(self.windows.len()); - self.windows.insert(window_id, window); - return window::spawn(window_id, Default::default()); - } - } - WindowMessage::Dragged(pane_grid::DragEvent::Dropped { - pane, - target, - }) => { - let window = self.windows.get_mut(&id).unwrap(); - window.panes.swap(&pane, &target); - } - WindowMessage::Dragged(_) => {} - WindowMessage::TogglePin(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(Pane { is_pinned, .. }) = - window.panes.get_mut(&pane) - { - *is_pinned = !*is_pinned; - } - } - WindowMessage::Close(pane) => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some((_, sibling)) = window.panes.close(&pane) { - window.focus = Some(sibling); - } - } - WindowMessage::CloseFocused => { - let window = self.windows.get_mut(&id).unwrap(); - if let Some(pane) = window.focus { - if let Some(Pane { is_pinned, .. }) = - window.panes.get(&pane) - { - if !is_pinned { - if let Some((_, sibling)) = - window.panes.close(&pane) - { - window.focus = Some(sibling); - } - } - } - } - } - }, - Message::CountIncremented(_) => { - self.count += 1; - } - } - - Command::none() - } - - fn subscription(&self) -> Subscription { - Subscription::batch(vec![ - subscription::events_with(|event, status| { - if let event::Status::Captured = status { - return None; - } - - match event { - Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) if modifiers.command() => { - handle_hotkey(key_code).map(|message| { - Message::Window(window::Id::new(0usize), message) - }) - } // TODO(derezzedex) - _ => None, - } - }), - time::every(Duration::from_secs(1)).map(Message::CountIncremented), - ]) - } - - fn view(&self, window: window::Id) -> Element { - let window_id = window; - - if let Some(window) = self.windows.get(&window) { - let focus = window.focus; - let total_panes = window.panes.len(); - - let window_controls = row![ - text_input( - "Window title", - &window.title, - WindowMessage::TitleChanged, - ), - button(text("Close")) - .on_press(WindowMessage::CloseWindow) - .style(theme::Button::Destructive), - ] - .spacing(5) - .align_items(Alignment::Center); - - let pane_grid = PaneGrid::new(&window.panes, |id, pane, _| { - let is_focused = focus == Some(id); - - let pin_button = button( - text(if pane.is_pinned { "Unpin" } else { "Pin" }).size(14), - ) - .on_press(WindowMessage::TogglePin(id)) - .padding(3); - - let title = row![ - pin_button, - "Pane", - text(pane.id.to_string()).style(if is_focused { - PANE_ID_COLOR_FOCUSED - } else { - PANE_ID_COLOR_UNFOCUSED - }), - ] - .spacing(5); - - let title_bar = pane_grid::TitleBar::new(title) - .controls(view_controls( - id, - total_panes, - pane.is_pinned, - pane.is_moving, - &window.title, - window_id, - &self.windows, - )) - .padding(10) - .style(if is_focused { - style::title_bar_focused - } else { - style::title_bar_active - }); - - pane_grid::Content::new(responsive(move |size| { - view_content( - id, - pane.scrollable_id.clone(), - self.count, - total_panes, - pane.is_pinned, - size, - ) - })) - .title_bar(title_bar) - .style(if is_focused { - style::pane_focused - } else { - style::pane_active - }) - }) - .width(Length::Fill) - .height(Length::Fill) - .spacing(10) - .on_click(WindowMessage::Clicked) - .on_drag(WindowMessage::Dragged) - .on_resize(10, WindowMessage::Resized); - - let content: Element<_> = column![window_controls, pane_grid] - .width(Length::Fill) - .height(Length::Fill) - .padding(10) - .into(); - - return content - .map(move |message| Message::Window(window_id, message)); - } - - container(text("This shouldn't be possible!").size(20)) - .center_x() - .center_y() - .into() - } - - fn close_requested(&self, window: window::Id) -> Self::Message { - Message::Window(window, WindowMessage::CloseWindow) - } - - fn scale_factor(&self, window: Id) -> f64 { - self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) - } - - fn theme(&self, window: Id) -> Theme { - self.windows.get(&window).expect("Window not found!").theme.clone() - } -} - -const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( - 0xFF as f32 / 255.0, - 0xC7 as f32 / 255.0, - 0xC7 as f32 / 255.0, -); -const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( - 0xFF as f32 / 255.0, - 0x47 as f32 / 255.0, - 0x47 as f32 / 255.0, -); - -fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { - use keyboard::KeyCode; - use pane_grid::{Axis, Direction}; - - let direction = match key_code { - KeyCode::Up => Some(Direction::Up), - KeyCode::Down => Some(Direction::Down), - KeyCode::Left => Some(Direction::Left), - KeyCode::Right => Some(Direction::Right), - _ => None, - }; - - match key_code { - KeyCode::V => Some(WindowMessage::SplitFocused(Axis::Vertical)), - KeyCode::H => Some(WindowMessage::SplitFocused(Axis::Horizontal)), - KeyCode::W => Some(WindowMessage::CloseFocused), - _ => direction.map(WindowMessage::FocusAdjacent), - } -} - -#[derive(Debug, Clone)] -struct SelectableWindow(window::Id, String); - -impl PartialEq for SelectableWindow { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -impl Eq for SelectableWindow {} - -impl std::fmt::Display for SelectableWindow { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.1.fmt(f) - } -} - -#[derive(Debug)] -struct Pane { - id: usize, - pub scrollable_id: scrollable::Id, - pub axis: pane_grid::Axis, - pub is_pinned: bool, - pub is_moving: bool, - pub snapped: bool, -} - -impl Pane { - fn new(id: usize, axis: pane_grid::Axis) -> Self { - Self { - id, - scrollable_id: scrollable::Id::unique(), - axis, - is_pinned: false, - is_moving: false, - snapped: false, - } - } -} - -fn view_content<'a>( - pane: pane_grid::Pane, - scrollable_id: scrollable::Id, - count: usize, - total_panes: usize, - is_pinned: bool, - size: Size, -) -> Element<'a, WindowMessage> { - let button = |label, message| { - button( - text(label) - .width(Length::Fill) - .horizontal_alignment(alignment::Horizontal::Center) - .size(16), - ) - .width(Length::Fill) - .padding(8) - .on_press(message) - }; - - let mut controls = column![ - button( - "Split horizontally", - WindowMessage::Split(pane_grid::Axis::Horizontal, pane), - ), - button( - "Split vertically", - WindowMessage::Split(pane_grid::Axis::Vertical, pane), - ), - button("Snap", WindowMessage::SnapToggle,) - ] - .spacing(5) - .max_width(150); - - if total_panes > 1 && !is_pinned { - controls = controls.push( - button("Close", WindowMessage::Close(pane)) - .style(theme::Button::Destructive), - ); - } - - let content = column![ - text(format!("{}x{}", size.width, size.height)).size(24), - controls, - text(format!("{count}")).size(48), - ] - .width(Length::Fill) - .height(800) - .spacing(10) - .align_items(Alignment::Center); - - container( - scrollable(content) - .height(Length::Fill) - .vertical_scroll(Properties::new()) - .id(scrollable_id), - ) - .width(Length::Fill) - .height(Length::Fill) - .padding(5) - .center_y() - .into() -} - -fn view_controls<'a>( - pane: pane_grid::Pane, - total_panes: usize, - is_pinned: bool, - is_moving: bool, - window_title: &'a str, - window_id: window::Id, - windows: &HashMap, -) -> Element<'a, WindowMessage> { - let window_selector = { - let options: Vec<_> = windows - .iter() - .map(|(id, window)| SelectableWindow(*id, window.title.clone())) - .collect(); - pick_list( - options, - Some(SelectableWindow(window_id, window_title.to_string())), - move |window| WindowMessage::SelectedWindow(pane, window), - ) - }; - - let mut move_to = button(text("Move to").size(14)).padding(3); - - let mut pop_out = button(text("Pop Out").size(14)).padding(3); - - let mut close = button(text("Close").size(14)) - .style(theme::Button::Destructive) - .padding(3); - - if total_panes > 1 && !is_pinned { - close = close.on_press(WindowMessage::Close(pane)); - pop_out = pop_out.on_press(WindowMessage::PopOut(pane)); - } - - if windows.len() > 1 && total_panes > 1 && !is_pinned { - move_to = move_to.on_press(WindowMessage::ToggleMoving(pane)); - } - - let mut content = row![].spacing(10); - if is_moving { - content = content.push(pop_out).push(window_selector).push(close); - } else { - content = content.push(pop_out).push(move_to).push(close); - } - - content.into() -} - -mod style { - use iced::widget::container; - use iced::Theme; - - pub fn title_bar_active(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - text_color: Some(palette.background.strong.text), - background: Some(palette.background.strong.color.into()), - ..Default::default() - } - } - - pub fn title_bar_focused(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - text_color: Some(palette.primary.strong.text), - background: Some(palette.primary.strong.color.into()), - ..Default::default() - } - } - - pub fn pane_active(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - background: Some(palette.background.weak.color.into()), - border_width: 2.0, - border_color: palette.background.strong.color, - ..Default::default() - } - } - - pub fn pane_focused(theme: &Theme) -> container::Appearance { - let palette = theme.extended_palette(); - - container::Appearance { - background: Some(palette.background.weak.color.into()), - border_width: 2.0, - border_color: palette.primary.strong.color, - ..Default::default() - } - } -} diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 83824535..7658384b 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,8 +1,8 @@ -use iced::alignment; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; use iced::widget::{button, column, container, image, row, text, text_input}; use iced::window::screenshot::{self, Screenshot}; +use iced::{alignment, window}; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, @@ -71,7 +71,10 @@ impl Application for Example { fn update(&mut self, message: Self::Message) -> Command { match message { Message::Screenshot => { - return iced::window::screenshot(Message::ScreenshotData); + return iced::window::screenshot( + window::Id::MAIN, + Message::ScreenshotData, + ); } Message::ScreenshotData(screenshot) => { self.screenshot = Some(screenshot); diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 4282ddcf..e28c4236 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -528,7 +528,9 @@ mod toast { clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, ) -> event::Status { - if let Event::Window(window::Event::RedrawRequested(now)) = &event { + if let Event::Window(_, window::Event::RedrawRequested(now)) = + &event + { let mut next_redraw: Option = None; self.instants.iter_mut().enumerate().for_each( diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 6ad7b4fb..04c8f618 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -164,7 +164,7 @@ impl Application for Todos { } } Message::ToggleFullscreen(mode) => { - window::change_mode(mode) + window::change_mode(window::Id::MAIN, mode) } _ => Command::none(), }; -- cgit From 83c7870c569a2976923ee6243a19813094d44673 Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 24 Jul 2023 14:32:59 -0700 Subject: Moved `exit_on_close_request` to window settings. This now controls whether each INDIVIDUAL window should close on CloseRequested events. --- examples/multi_window/src/main.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 58604702..51ec3595 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -8,10 +8,7 @@ use iced::{ use std::collections::HashMap; fn main() -> iced::Result { - Example::run(Settings { - exit_on_close_request: false, - ..Default::default() - }) + Example::run(Settings::default()) } #[derive(Default)] @@ -111,6 +108,7 @@ impl multi_window::Application for Example { id, window::Settings { position: self.next_window_pos, + exit_on_close_request: count % 2 == 0, ..Default::default() }, ); -- cgit From 67408311f45d341509538f8cc185978da66b6ace Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Nov 2023 23:40:33 +0100 Subject: Use actual floats for logical coordinates --- examples/multi_window/src/main.rs | 15 +++++++++------ examples/solar_system/src/main.rs | 14 +++++--------- examples/todos/src/main.rs | 4 ++-- 3 files changed, 16 insertions(+), 17 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 7d1f1e91..16beb46e 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -4,8 +4,10 @@ use iced::multi_window::{self, Application}; use iced::widget::{button, column, container, scrollable, text, text_input}; use iced::window; use iced::{ - Alignment, Command, Element, Length, Settings, Subscription, Theme, + Alignment, Command, Element, Length, Point, Settings, Subscription, Theme, + Vector, }; + use std::collections::HashMap; fn main() -> iced::Result { @@ -33,8 +35,8 @@ enum Message { ScaleChanged(window::Id, String), TitleChanged(window::Id, String), CloseWindow(window::Id), + WindowCreated(window::Id, Option), WindowDestroyed(window::Id), - WindowCreated(window::Id, (i32, i32)), NewWindow, } @@ -90,10 +92,11 @@ impl multi_window::Application for Example { self.windows.remove(&id); } Message::WindowCreated(id, position) => { - self.next_window_pos = window::Position::Specific( - position.0 + 20, - position.1 + 20, - ); + if let Some(position) = position { + self.next_window_pos = window::Position::Specific( + position + Vector::new(20.0, 20.0), + ); + } if let Some(window) = self.windows.get(&id) { return text_input::focus(window.input_id.clone()); diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8295dded..82421a86 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -114,14 +114,14 @@ impl State { pub fn new() -> State { let now = Instant::now(); - let (width, height) = window::Settings::default().size; + let size = window::Settings::default().size; State { space_cache: canvas::Cache::default(), system_cache: canvas::Cache::default(), start: now, now, - stars: Self::generate_stars(width, height), + stars: Self::generate_stars(size.width, size.height), } } @@ -130,7 +130,7 @@ impl State { self.system_cache.clear(); } - fn generate_stars(width: u32, height: u32) -> Vec<(Point, f32)> { + fn generate_stars(width: f32, height: f32) -> Vec<(Point, f32)> { use rand::Rng; let mut rng = rand::thread_rng(); @@ -139,12 +139,8 @@ impl State { .map(|_| { ( Point::new( - rng.gen_range( - (-(width as f32) / 2.0)..(width as f32 / 2.0), - ), - rng.gen_range( - (-(height as f32) / 2.0)..(height as f32 / 2.0), - ), + rng.gen_range((-width / 2.0)..(width / 2.0)), + rng.gen_range((-height / 2.0)..(height / 2.0)), ), rng.gen_range(0.5..1.0), ) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index a7ba69b9..4dac032c 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -8,7 +8,7 @@ use iced::widget::{ }; use iced::window; use iced::{Application, Element}; -use iced::{Color, Command, Length, Settings, Subscription}; +use iced::{Color, Command, Length, Settings, Size, Subscription}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; @@ -22,7 +22,7 @@ pub fn main() -> iced::Result { Todos::run(Settings { window: window::Settings { - size: (500, 800), + size: Size::new(500.0, 800.0), ..window::Settings::default() }, ..Settings::default() -- cgit From ea42af766f345715ff7a7168182d3896ee79cfbc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 2 Dec 2023 20:41:58 +0100 Subject: Use `AtomicU64` for `window::Id` --- examples/multi_window/src/main.rs | 49 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index 16beb46e..5a5e70c1 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -35,8 +35,8 @@ enum Message { ScaleChanged(window::Id, String), TitleChanged(window::Id, String), CloseWindow(window::Id), - WindowCreated(window::Id, Option), - WindowDestroyed(window::Id), + WindowOpened(window::Id, Option), + WindowClosed(window::Id), NewWindow, } @@ -69,6 +69,8 @@ impl multi_window::Application for Example { let window = self.windows.get_mut(&id).expect("Window not found!"); window.scale_input = scale; + + Command::none() } Message::ScaleChanged(id, scale) => { let window = @@ -78,20 +80,23 @@ impl multi_window::Application for Example { .parse::() .unwrap_or(window.current_scale) .clamp(0.5, 5.0); + + Command::none() } Message::TitleChanged(id, title) => { let window = self.windows.get_mut(&id).expect("Window not found."); window.title = title; + + Command::none() } - Message::CloseWindow(id) => { - return window::close(id); - } - Message::WindowDestroyed(id) => { + Message::CloseWindow(id) => window::close(id), + Message::WindowClosed(id) => { self.windows.remove(&id); + Command::none() } - Message::WindowCreated(id, position) => { + Message::WindowOpened(id, position) => { if let Some(position) = position { self.next_window_pos = window::Position::Specific( position + Vector::new(20.0, 20.0), @@ -99,27 +104,25 @@ impl multi_window::Application for Example { } if let Some(window) = self.windows.get(&id) { - return text_input::focus(window.input_id.clone()); + text_input::focus(window.input_id.clone()) + } else { + Command::none() } } Message::NewWindow => { let count = self.windows.len() + 1; - let id = window::Id::new(count); + + let (id, spawn_window) = window::spawn(window::Settings { + position: self.next_window_pos, + exit_on_close_request: count % 2 == 0, + ..Default::default() + }); self.windows.insert(id, Window::new(count)); - return window::spawn( - id, - window::Settings { - position: self.next_window_pos, - exit_on_close_request: count % 2 == 0, - ..Default::default() - }, - ); + spawn_window } } - - Command::none() } fn view(&self, window: window::Id) -> Element { @@ -151,12 +154,10 @@ impl multi_window::Application for Example { window::Event::CloseRequested => { Some(Message::CloseWindow(id)) } - window::Event::Destroyed => { - Some(Message::WindowDestroyed(id)) - } - window::Event::Created { position, .. } => { - Some(Message::WindowCreated(id, position)) + window::Event::Opened { position, .. } => { + Some(Message::WindowOpened(id, position)) } + window::Event::Closed => Some(Message::WindowClosed(id)), _ => None, } } else { -- cgit