From a79603e4ca5e0cee46a737ef0b1af5c69ddb49b6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Mar 2020 06:32:56 +0100 Subject: Rename `Split` to `Axis` --- native/src/widget/pane_grid.rs | 4 +-- native/src/widget/pane_grid/axis.rs | 54 ++++++++++++++++++++++++++++++++++++ native/src/widget/pane_grid/node.rs | 12 ++++---- native/src/widget/pane_grid/split.rs | 54 ------------------------------------ native/src/widget/pane_grid/state.rs | 15 ++++------ 5 files changed, 67 insertions(+), 72 deletions(-) create mode 100644 native/src/widget/pane_grid/axis.rs delete mode 100644 native/src/widget/pane_grid/split.rs (limited to 'native/src/widget') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index e446b235..12d0b09d 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -1,12 +1,12 @@ +mod axis; mod direction; mod node; mod pane; -mod split; mod state; +pub use axis::Axis; pub use direction::Direction; pub use pane::Pane; -pub use split::Split; pub use state::{Focus, State}; use crate::{ diff --git a/native/src/widget/pane_grid/axis.rs b/native/src/widget/pane_grid/axis.rs new file mode 100644 index 00000000..375509b7 --- /dev/null +++ b/native/src/widget/pane_grid/axis.rs @@ -0,0 +1,54 @@ +use crate::Rectangle; + +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +pub enum Axis { + Horizontal, + Vertical, +} + +impl Axis { + pub(super) fn split( + &self, + rectangle: &Rectangle, + ratio: f32, + halved_spacing: f32, + ) -> (Rectangle, Rectangle) { + match self { + Axis::Horizontal => { + let width_left = + (rectangle.width * ratio).round() - halved_spacing; + let width_right = rectangle.width - width_left - halved_spacing; + + ( + Rectangle { + width: width_left, + ..*rectangle + }, + Rectangle { + x: rectangle.x + width_left + halved_spacing, + width: width_right, + ..*rectangle + }, + ) + } + Axis::Vertical => { + let height_top = + (rectangle.height * ratio).round() - halved_spacing; + let height_bottom = + rectangle.height - height_top - halved_spacing; + + ( + Rectangle { + height: height_top, + ..*rectangle + }, + Rectangle { + y: rectangle.y + height_top + halved_spacing, + height: height_bottom, + ..*rectangle + }, + ) + } + } + } +} diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs index 744e3e17..aaf775d8 100644 --- a/native/src/widget/pane_grid/node.rs +++ b/native/src/widget/pane_grid/node.rs @@ -1,5 +1,5 @@ use crate::{ - pane_grid::{Pane, Split}, + pane_grid::{Axis, Pane}, Rectangle, Size, }; @@ -9,7 +9,7 @@ use std::collections::HashMap; pub enum Node { Split { id: usize, - kind: Split, + axis: Axis, ratio: u32, a: Box, b: Box, @@ -33,10 +33,10 @@ impl Node { } } - pub fn split(&mut self, id: usize, kind: Split, new_pane: Pane) { + pub fn split(&mut self, id: usize, axis: Axis, new_pane: Pane) { *self = Node::Split { id, - kind, + axis, ratio: 500_000, a: Box::new(self.clone()), b: Box::new(Node::Pane(new_pane)), @@ -115,11 +115,11 @@ impl Node { ) { match self { Node::Split { - kind, ratio, a, b, .. + axis, ratio, a, b, .. } => { let ratio = *ratio as f32 / 1_000_000.0; let (region_a, region_b) = - kind.apply(current, ratio, halved_spacing); + axis.split(current, ratio, halved_spacing); a.compute_regions(halved_spacing, ®ion_a, regions); b.compute_regions(halved_spacing, ®ion_b, regions); diff --git a/native/src/widget/pane_grid/split.rs b/native/src/widget/pane_grid/split.rs deleted file mode 100644 index ca9ed5e1..00000000 --- a/native/src/widget/pane_grid/split.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::Rectangle; - -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub enum Split { - Horizontal, - Vertical, -} - -impl Split { - pub(super) fn apply( - &self, - rectangle: &Rectangle, - ratio: f32, - halved_spacing: f32, - ) -> (Rectangle, Rectangle) { - match self { - Split::Horizontal => { - let width_left = - (rectangle.width * ratio).round() - halved_spacing; - let width_right = rectangle.width - width_left - halved_spacing; - - ( - Rectangle { - width: width_left, - ..*rectangle - }, - Rectangle { - x: rectangle.x + width_left + halved_spacing, - width: width_right, - ..*rectangle - }, - ) - } - Split::Vertical => { - let height_top = - (rectangle.height * ratio).round() - halved_spacing; - let height_bottom = - rectangle.height - height_top - halved_spacing; - - ( - Rectangle { - height: height_top, - ..*rectangle - }, - Rectangle { - y: rectangle.y + height_top + halved_spacing, - height: height_bottom, - ..*rectangle - }, - ) - } - } - } -} diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 61576a29..dc66e32a 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -1,6 +1,6 @@ use crate::{ input::keyboard, - pane_grid::{node::Node, Direction, Pane, Split}, + pane_grid::{node::Node, Axis, Direction, Pane}, Hasher, Point, Rectangle, Size, }; @@ -99,7 +99,7 @@ impl State { } pub fn split_vertically(&mut self, pane: &Pane, state: T) -> Option { - self.split(Split::Vertical, pane, state) + self.split(Axis::Vertical, pane, state) } pub fn split_horizontally( @@ -107,15 +107,10 @@ impl State { pane: &Pane, state: T, ) -> Option { - self.split(Split::Horizontal, pane, state) + self.split(Axis::Horizontal, pane, state) } - pub fn split( - &mut self, - kind: Split, - pane: &Pane, - state: T, - ) -> Option { + pub fn split(&mut self, axis: Axis, pane: &Pane, state: T) -> Option { let node = self.internal.layout.find(pane)?; let new_pane = { @@ -130,7 +125,7 @@ impl State { self.internal.last_id }; - node.split(split_id, kind, new_pane); + node.split(split_id, axis, new_pane); let _ = self.panes.insert(new_pane, state); self.focus(&new_pane); -- cgit