summaryrefslogtreecommitdiffstats
path: root/examples/pane_grid
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pane_grid')
-rw-r--r--examples/pane_grid/Cargo.toml3
-rw-r--r--examples/pane_grid/README.md4
-rw-r--r--examples/pane_grid/src/main.rs152
3 files changed, 113 insertions, 46 deletions
diff --git a/examples/pane_grid/Cargo.toml b/examples/pane_grid/Cargo.toml
index 3ed912ac..e489f210 100644
--- a/examples/pane_grid/Cargo.toml
+++ b/examples/pane_grid/Cargo.toml
@@ -6,4 +6,5 @@ edition = "2018"
publish = false
[dependencies]
-iced = { path = "../.." }
+iced = { path = "../..", features = ["debug"] }
+iced_native = { path = "../../native" }
diff --git a/examples/pane_grid/README.md b/examples/pane_grid/README.md
index 3653fc5b..a4cfcb7d 100644
--- a/examples/pane_grid/README.md
+++ b/examples/pane_grid/README.md
@@ -15,8 +15,8 @@ This example showcases the `PaneGrid` widget, which features:
The __[`main`]__ file contains all the code of the example.
<div align="center">
- <a href="https://gfycat.com/mixedflatjellyfish">
- <img src="https://thumbs.gfycat.com/MixedFlatJellyfish-small.gif">
+ <a href="https://gfycat.com/frailfreshairedaleterrier">
+ <img src="https://thumbs.gfycat.com/FrailFreshAiredaleterrier-small.gif">
</a>
</div>
diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs
index b4bbd68f..3c3256cf 100644
--- a/examples/pane_grid/src/main.rs
+++ b/examples/pane_grid/src/main.rs
@@ -1,16 +1,18 @@
use iced::{
- button, keyboard, pane_grid, scrollable, Align, Button, Column, Container,
- Element, HorizontalAlignment, Length, PaneGrid, Sandbox, Scrollable,
- Settings, Text,
+ button, executor, keyboard, pane_grid, scrollable, Align, Application,
+ Button, Column, Command, Container, Element, HorizontalAlignment, Length,
+ PaneGrid, Scrollable, Settings, Subscription, Text,
};
+use iced_native::{event, subscription, Event};
-pub fn main() {
+pub fn main() -> iced::Result {
Example::run(Settings::default())
}
struct Example {
panes: pane_grid::State<Content>,
panes_created: usize,
+ focus: Option<pane_grid::Pane>,
}
#[derive(Debug, Clone, Copy)]
@@ -18,59 +20,77 @@ enum Message {
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),
Close(pane_grid::Pane),
CloseFocused,
}
-impl Sandbox for Example {
+impl Application for Example {
type Message = Message;
+ type Executor = executor::Default;
+ type Flags = ();
- fn new() -> Self {
+ fn new(_flags: ()) -> (Self, Command<Message>) {
let (panes, _) = pane_grid::State::new(Content::new(0));
- Example {
- panes,
- panes_created: 1,
- }
+ (
+ Example {
+ panes,
+ panes_created: 1,
+ focus: None,
+ },
+ Command::none(),
+ )
}
fn title(&self) -> String {
String::from("Pane grid - Iced")
}
- fn update(&mut self, message: Message) {
+ fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::Split(axis, pane) => {
- let _ = self.panes.split(
+ let result = self.panes.split(
axis,
&pane,
Content::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.panes.active() {
- let _ = self.panes.split(
+ if let Some(pane) = self.focus {
+ let result = self.panes.split(
axis,
&pane,
Content::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.panes.active() {
+ if let Some(pane) = self.focus {
if let Some(adjacent) =
self.panes.adjacent(&pane, direction)
{
- self.panes.focus(&adjacent);
+ self.focus = Some(adjacent);
}
}
}
+ Message::Clicked(pane) => {
+ self.focus = Some(pane);
+ }
Message::Resized(pane_grid::ResizeEvent { split, ratio }) => {
self.panes.resize(&split, ratio);
}
@@ -82,29 +102,60 @@ impl Sandbox for Example {
}
Message::Dragged(_) => {}
Message::Close(pane) => {
- let _ = self.panes.close(&pane);
+ if let Some((_, sibling)) = self.panes.close(&pane) {
+ self.focus = Some(sibling);
+ }
}
Message::CloseFocused => {
- if let Some(pane) = self.panes.active() {
- let _ = self.panes.close(&pane);
+ if let Some(pane) = self.focus {
+ if let Some((_, sibling)) = self.panes.close(&pane) {
+ self.focus = Some(sibling);
+ }
}
}
}
+
+ Command::none()
+ }
+
+ fn subscription(&self) -> Subscription<Message> {
+ 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.is_command_pressed() => handle_hotkey(key_code),
+ _ => None,
+ }
+ })
}
fn view(&mut self) -> Element<Message> {
+ let focus = self.focus;
let total_panes = self.panes.len();
- let pane_grid =
- PaneGrid::new(&mut self.panes, |pane, content, focus| {
- content.view(pane, focus, total_panes)
- })
- .width(Length::Fill)
- .height(Length::Fill)
- .spacing(10)
- .on_drag(Message::Dragged)
- .on_resize(Message::Resized)
- .on_key_press(handle_hotkey);
+ let pane_grid = PaneGrid::new(&mut self.panes, |pane, content| {
+ let is_focused = focus == Some(pane);
+
+ let title_bar =
+ pane_grid::TitleBar::new(format!("Pane {}", content.id))
+ .padding(10)
+ .style(style::TitleBar { is_focused });
+
+ pane_grid::Content::new(content.view(pane, total_panes))
+ .title_bar(title_bar)
+ .style(style::Pane { is_focused })
+ })
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .spacing(10)
+ .on_click(Message::Clicked)
+ .on_drag(Message::Dragged)
+ .on_resize(10, Message::Resized);
Container::new(pane_grid)
.width(Length::Fill)
@@ -114,11 +165,11 @@ impl Sandbox for Example {
}
}
-fn handle_hotkey(event: pane_grid::KeyPressEvent) -> Option<Message> {
+fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
use keyboard::KeyCode;
use pane_grid::{Axis, Direction};
- let direction = match event.key_code {
+ let direction = match key_code {
KeyCode::Up => Some(Direction::Up),
KeyCode::Down => Some(Direction::Down),
KeyCode::Left => Some(Direction::Left),
@@ -126,7 +177,7 @@ fn handle_hotkey(event: pane_grid::KeyPressEvent) -> Option<Message> {
_ => None,
};
- match event.key_code {
+ match key_code {
KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)),
KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)),
KeyCode::W => Some(Message::CloseFocused),
@@ -155,15 +206,14 @@ impl Content {
fn view(
&mut self,
pane: pane_grid::Pane,
- focus: Option<pane_grid::Focus>,
total_panes: usize,
) -> Element<Message> {
let Content {
- id,
scroll,
split_horizontally,
split_vertically,
close,
+ ..
} = self;
let button = |state, label, message, style| {
@@ -209,7 +259,6 @@ impl Content {
.width(Length::Fill)
.spacing(10)
.align_items(Align::Center)
- .push(Text::new(format!("Pane {}", id)).size(30))
.push(controls);
Container::new(content)
@@ -217,9 +266,6 @@ impl Content {
.height(Length::Fill)
.padding(5)
.center_y()
- .style(style::Pane {
- is_focused: focus.is_some(),
- })
.into()
}
}
@@ -245,6 +291,25 @@ mod style {
0xC4 as f32 / 255.0,
);
+ pub struct TitleBar {
+ pub is_focused: bool,
+ }
+
+ impl container::StyleSheet for TitleBar {
+ fn style(&self) -> container::Style {
+ let pane = Pane {
+ is_focused: self.is_focused,
+ }
+ .style();
+
+ container::Style {
+ text_color: Some(Color::WHITE),
+ background: Some(pane.border_color.into()),
+ ..Default::default()
+ }
+ }
+ }
+
pub struct Pane {
pub is_focused: bool,
}
@@ -253,10 +318,11 @@ mod style {
fn style(&self) -> container::Style {
container::Style {
background: Some(Background::Color(SURFACE)),
- border_width: 2,
- border_color: Color {
- a: if self.is_focused { 1.0 } else { 0.3 },
- ..Color::BLACK
+ border_width: 2.0,
+ border_color: if self.is_focused {
+ Color::BLACK
+ } else {
+ Color::from_rgb(0.7, 0.7, 0.7)
},
..Default::default()
}
@@ -280,7 +346,7 @@ mod style {
button::Style {
text_color,
background: background.map(Background::Color),
- border_radius: 5,
+ border_radius: 5.0,
shadow_offset: Vector::new(0.0, 0.0),
..button::Style::default()
}