1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

exposing Side publicly and renaming to PanelSide to avoid conflict

This commit is contained in:
Bruno Paré-Simard
2025-01-30 14:42:37 -05:00
parent db99eb3187
commit eca13ab379
3 changed files with 61 additions and 61 deletions

View File

@@ -21,7 +21,7 @@ pub use {
combo_box::*, combo_box::*,
frame::Frame, frame::Frame,
modal::{Modal, ModalResponse}, modal::{Modal, ModalResponse},
panel::{CentralPanel, Panel}, panel::{CentralPanel, Panel, PanelSide, VerticalSide, HorizontalSide},
popup::*, popup::*,
resize::Resize, resize::Resize,
scene::Scene, scene::Scene,

View File

@@ -64,32 +64,32 @@ pub enum HorizontalSide {
Bottom, Bottom,
} }
/// [`Horizontal`](Side::Horizontal) or [`Vertical`](Side::Vertical) /// [`Horizontal`](PanelSide::Horizontal) or [`Vertical`](PanelSide::Vertical)
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Side { pub enum PanelSide {
Vertical(VerticalSide), Vertical(VerticalSide),
Horizontal(HorizontalSide), Horizontal(HorizontalSide),
} }
impl Side { impl PanelSide {
fn opposite(self) -> Self { fn opposite(self) -> Self {
let opposite_vertical = |side: VerticalSide| -> Side { let opposite_vertical = |side: VerticalSide| -> PanelSide {
match side { match side {
VerticalSide::Left => Side::Vertical(VerticalSide::Right), VerticalSide::Left => PanelSide::Vertical(VerticalSide::Right),
VerticalSide::Right => Side::Vertical(VerticalSide::Left), VerticalSide::Right => PanelSide::Vertical(VerticalSide::Left),
} }
}; };
let opposite_horizontal = |side: HorizontalSide| -> Side { let opposite_horizontal = |side: HorizontalSide| -> PanelSide {
match side { match side {
HorizontalSide::Top => Side::Horizontal(HorizontalSide::Bottom), HorizontalSide::Top => PanelSide::Horizontal(HorizontalSide::Bottom),
HorizontalSide::Bottom => Side::Horizontal(HorizontalSide::Top), HorizontalSide::Bottom => PanelSide::Horizontal(HorizontalSide::Top),
} }
}; };
match self { match self {
Side::Vertical(side) => opposite_vertical(side), PanelSide::Vertical(side) => opposite_vertical(side),
Side::Horizontal(side) => opposite_horizontal(side), PanelSide::Horizontal(side) => opposite_horizontal(side),
} }
} }
@@ -105,8 +105,8 @@ impl Side {
}; };
match self { match self {
Side::Vertical(side) => set_rect_size_vertical(side), PanelSide::Vertical(side) => set_rect_size_vertical(side),
Side::Horizontal(side) => set_rect_size_horizontal(side), PanelSide::Horizontal(side) => set_rect_size_horizontal(side),
} }
} }
@@ -126,8 +126,8 @@ impl Side {
}; };
match self { match self {
Side::Vertical(side) => side_axe_vertical(side, rect), PanelSide::Vertical(side) => side_axe_vertical(side, rect),
Side::Horizontal(side) => side_axe_horizontal(side, rect), PanelSide::Horizontal(side) => side_axe_horizontal(side, rect),
} }
} }
@@ -147,8 +147,8 @@ impl Side {
}; };
match self { match self {
Side::Vertical(side) => sign_vertical(side), PanelSide::Vertical(side) => sign_vertical(side),
Side::Horizontal(side) => sign_horizontal(side), PanelSide::Horizontal(side) => sign_horizontal(side),
} }
} }
} }
@@ -185,15 +185,15 @@ impl<'a> PanelSizer<'a> {
fn get_size_from_state_or_default(panel: &Panel, ui: &mut Ui, frame: Frame) -> f32 { fn get_size_from_state_or_default(panel: &Panel, ui: &mut Ui, frame: Frame) -> f32 {
if let Some(state) = PanelState::load(ui.ctx(), panel.id) { if let Some(state) = PanelState::load(ui.ctx(), panel.id) {
match panel.side { match panel.side {
Side::Vertical(_) => state.rect.width(), PanelSide::Vertical(_) => state.rect.width(),
Side::Horizontal(_) => state.rect.height(), PanelSide::Horizontal(_) => state.rect.height(),
} }
} else { } else {
match panel.side { match panel.side {
Side::Vertical(_) => panel.default_size.unwrap_or_else(|| { PanelSide::Vertical(_) => panel.default_size.unwrap_or_else(|| {
ui.style().spacing.interact_size.x + frame.inner_margin.sum().x ui.style().spacing.interact_size.x + frame.inner_margin.sum().x
}), }),
Side::Horizontal(_) => panel.default_size.unwrap_or_else(|| { PanelSide::Horizontal(_) => panel.default_size.unwrap_or_else(|| {
ui.style().spacing.interact_size.y + frame.inner_margin.sum().y ui.style().spacing.interact_size.y + frame.inner_margin.sum().y
}), }),
} }
@@ -207,10 +207,10 @@ impl<'a> PanelSizer<'a> {
let mut panel_rect = available_rect; let mut panel_rect = available_rect;
match side { match side {
Side::Vertical(_) => { PanelSide::Vertical(_) => {
size = clamp_to_range(size, size_range).at_most(available_rect.width()); size = clamp_to_range(size, size_range).at_most(available_rect.width());
} }
Side::Horizontal(_) => { PanelSide::Horizontal(_) => {
size = clamp_to_range(size, size_range).at_most(available_rect.height()); size = clamp_to_range(size, size_range).at_most(available_rect.height());
} }
} }
@@ -226,12 +226,12 @@ impl<'a> PanelSizer<'a> {
let pointer = pointer.unwrap(); let pointer = pointer.unwrap();
match side { match side {
Side::Vertical(_) => { PanelSide::Vertical(_) => {
self.size = (pointer.x - side.side_axe(self.panel_rect)).abs(); self.size = (pointer.x - side.side_axe(self.panel_rect)).abs();
self.size = self.size =
clamp_to_range(self.size, size_range).at_most(self.available_rect.width()); clamp_to_range(self.size, size_range).at_most(self.available_rect.width());
} }
Side::Horizontal(_) => { PanelSide::Horizontal(_) => {
self.size = (pointer.y - side.side_axe(self.panel_rect)).abs(); self.size = (pointer.y - side.side_axe(self.panel_rect)).abs();
self.size = self.size =
clamp_to_range(self.size, size_range).at_most(self.available_rect.height()); clamp_to_range(self.size, size_range).at_most(self.available_rect.height());
@@ -266,7 +266,7 @@ impl<'a> PanelSizer<'a> {
/// ``` /// ```
#[must_use = "You should call .show()"] #[must_use = "You should call .show()"]
pub struct Panel { pub struct Panel {
side: Side, side: PanelSide,
id: Id, id: Id,
frame: Option<Frame>, frame: Option<Frame>,
resizable: bool, resizable: bool,
@@ -286,42 +286,42 @@ impl Panel {
/// ///
/// The id should be globally unique, e.g. `Id::new("my_left_panel")`. /// The id should be globally unique, e.g. `Id::new("my_left_panel")`.
pub fn left(id: impl Into<Id>) -> Self { pub fn left(id: impl Into<Id>) -> Self {
Self::new(Side::Vertical(VerticalSide::Left), id) Self::new(PanelSide::Vertical(VerticalSide::Left), id)
} }
/// Create a right panel. /// Create a right panel.
/// ///
/// The id should be globally unique, e.g. `Id::new("my_right_panel")`. /// The id should be globally unique, e.g. `Id::new("my_right_panel")`.
pub fn right(id: impl Into<Id>) -> Self { pub fn right(id: impl Into<Id>) -> Self {
Self::new(Side::Vertical(VerticalSide::Right), id) Self::new(PanelSide::Vertical(VerticalSide::Right), id)
} }
/// Create a top panel. /// Create a top panel.
/// ///
/// The id should be globally unique, e.g. `Id::new("my_top_panel")`. /// The id should be globally unique, e.g. `Id::new("my_top_panel")`.
pub fn top(id: impl Into<Id>) -> Self { pub fn top(id: impl Into<Id>) -> Self {
Self::new(Side::Horizontal(HorizontalSide::Top), id) Self::new(PanelSide::Horizontal(HorizontalSide::Top), id)
} }
/// Create a bottom panel. /// Create a bottom panel.
/// ///
/// The id should be globally unique, e.g. `Id::new("my_bottom_panel")`. /// The id should be globally unique, e.g. `Id::new("my_bottom_panel")`.
pub fn bottom(id: impl Into<Id>) -> Self { pub fn bottom(id: impl Into<Id>) -> Self {
Self::new(Side::Horizontal(HorizontalSide::Bottom), id) Self::new(PanelSide::Horizontal(HorizontalSide::Bottom), id)
} }
/// Create a panel. /// Create a panel.
/// ///
/// The id should be globally unique, e.g. `Id::new("my_panel")`. /// The id should be globally unique, e.g. `Id::new("my_panel")`.
pub fn new(side: Side, id: impl Into<Id>) -> Self { pub fn new(side: PanelSide, id: impl Into<Id>) -> Self {
let default_size: Option<f32> = match side { let default_size: Option<f32> = match side {
Side::Vertical(_) => Some(200.0), PanelSide::Vertical(_) => Some(200.0),
Side::Horizontal(_) => None, PanelSide::Horizontal(_) => None,
}; };
let size_range: Rangef = match side { let size_range: Rangef = match side {
Side::Vertical(_) => Rangef::new(96.0, f32::INFINITY), PanelSide::Vertical(_) => Rangef::new(96.0, f32::INFINITY),
Side::Horizontal(_) => Rangef::new(20.0, f32::INFINITY), PanelSide::Horizontal(_) => Rangef::new(20.0, f32::INFINITY),
}; };
Self { Self {
@@ -567,11 +567,11 @@ impl Panel {
panel_sizer.panel_rect = panel_sizer.panel_rect.round_ui(); panel_sizer.panel_rect = panel_sizer.panel_rect.round_ui();
let get_ui_kind = || match side { let get_ui_kind = || match side {
Side::Vertical(v_side) => match v_side { PanelSide::Vertical(v_side) => match v_side {
VerticalSide::Left => UiKind::LeftPanel, VerticalSide::Left => UiKind::LeftPanel,
VerticalSide::Right => UiKind::RightPanel, VerticalSide::Right => UiKind::RightPanel,
}, },
Side::Horizontal(h_side) => match h_side { PanelSide::Horizontal(h_side) => match h_side {
HorizontalSide::Top => UiKind::TopPanel, HorizontalSide::Top => UiKind::TopPanel,
HorizontalSide::Bottom => UiKind::BottomPanel, HorizontalSide::Bottom => UiKind::BottomPanel,
}, },
@@ -589,13 +589,13 @@ impl Panel {
let inner_response = panel_sizer.frame.show(&mut panel_ui, |ui| { let inner_response = panel_sizer.frame.show(&mut panel_ui, |ui| {
match side { match side {
Side::Vertical(_) => { PanelSide::Vertical(_) => {
ui.set_min_height(ui.max_rect().height()); // Make sure the frame fills the full height ui.set_min_height(ui.max_rect().height()); // Make sure the frame fills the full height
ui.set_min_width( ui.set_min_width(
(size_range.min - panel_sizer.frame.inner_margin.sum().x).at_least(0.0), (size_range.min - panel_sizer.frame.inner_margin.sum().x).at_least(0.0),
); );
} }
Side::Horizontal(_) => { PanelSide::Horizontal(_) => {
ui.set_min_width(ui.max_rect().width()); // Make the frame fill full width ui.set_min_width(ui.max_rect().width()); // Make the frame fill full width
ui.set_min_height( ui.set_min_height(
(size_range.min - panel_sizer.frame.inner_margin.sum().y).at_least(0.0), (size_range.min - panel_sizer.frame.inner_margin.sum().y).at_least(0.0),
@@ -611,11 +611,11 @@ impl Panel {
{ {
let mut cursor = ui.cursor(); let mut cursor = ui.cursor();
match side { match side {
Side::Vertical(v_side) => match v_side { PanelSide::Vertical(v_side) => match v_side {
VerticalSide::Left => cursor.min.x = rect.max.x, VerticalSide::Left => cursor.min.x = rect.max.x,
VerticalSide::Right => cursor.max.x = rect.min.x, VerticalSide::Right => cursor.max.x = rect.min.x,
}, },
Side::Horizontal(h_side) => match h_side { PanelSide::Horizontal(h_side) => match h_side {
HorizontalSide::Top => cursor.min.y = rect.max.y, HorizontalSide::Top => cursor.min.y = rect.max.y,
HorizontalSide::Bottom => cursor.max.y = rect.min.y, HorizontalSide::Bottom => cursor.max.y = rect.min.y,
}, },
@@ -655,11 +655,11 @@ impl Panel {
let resize_axe = side.opposite().side_axe(rect); let resize_axe = side.opposite().side_axe(rect);
let resize_axe = resize_axe + 0.5 * side.sign() * stroke.width; let resize_axe = resize_axe + 0.5 * side.sign() * stroke.width;
match side { match side {
Side::Vertical(_) => { PanelSide::Vertical(_) => {
ui.painter() ui.painter()
.vline(resize_axe, panel_sizer.panel_rect.y_range(), stroke); .vline(resize_axe, panel_sizer.panel_rect.y_range(), stroke);
} }
Side::Horizontal(_) => { PanelSide::Horizontal(_) => {
ui.painter() ui.painter()
.hline(panel_sizer.panel_rect.x_range(), resize_axe, stroke); .hline(panel_sizer.panel_rect.x_range(), resize_axe, stroke);
} }
@@ -690,7 +690,7 @@ impl Panel {
let rect = inner_response.response.rect; let rect = inner_response.response.rect;
match side { match side {
Side::Vertical(v_side) => match v_side { PanelSide::Vertical(v_side) => match v_side {
VerticalSide::Left => ctx.pass_state_mut(|state| { VerticalSide::Left => ctx.pass_state_mut(|state| {
state.allocate_left_panel(Rect::from_min_max(available_rect.min, rect.max)); state.allocate_left_panel(Rect::from_min_max(available_rect.min, rect.max));
}), }),
@@ -698,7 +698,7 @@ impl Panel {
state.allocate_right_panel(Rect::from_min_max(rect.min, available_rect.max)); state.allocate_right_panel(Rect::from_min_max(rect.min, available_rect.max));
}), }),
}, },
Side::Horizontal(h_side) => match h_side { PanelSide::Horizontal(h_side) => match h_side {
HorizontalSide::Top => { HorizontalSide::Top => {
ctx.pass_state_mut(|state| { ctx.pass_state_mut(|state| {
state.allocate_top_panel(Rect::from_min_max(available_rect.min, rect.max)); state.allocate_top_panel(Rect::from_min_max(available_rect.min, rect.max));
@@ -734,7 +734,7 @@ impl Panel {
fn resize_panel(&self, panel_sizer: &mut PanelSizer<'_>, ui: &mut Ui) -> (bool, bool) { fn resize_panel(&self, panel_sizer: &mut PanelSizer<'_>, ui: &mut Ui) -> (bool, bool) {
let (resize_x, resize_y, amount): (Rangef, Rangef, Vec2) = match self.side { let (resize_x, resize_y, amount): (Rangef, Rangef, Vec2) = match self.side {
Side::Vertical(_) => { PanelSide::Vertical(_) => {
let resize_x = self.side.opposite().side_axe(panel_sizer.panel_rect); let resize_x = self.side.opposite().side_axe(panel_sizer.panel_rect);
let resize_y = panel_sizer.panel_rect.y_range(); let resize_y = panel_sizer.panel_rect.y_range();
( (
@@ -743,7 +743,7 @@ impl Panel {
vec2(ui.style().interaction.resize_grab_radius_side, 0.0), vec2(ui.style().interaction.resize_grab_radius_side, 0.0),
) )
} }
Side::Horizontal(_) => { PanelSide::Horizontal(_) => {
let resize_x = panel_sizer.panel_rect.x_range(); let resize_x = panel_sizer.panel_rect.x_range();
let resize_y = self.side.opposite().side_axe(panel_sizer.panel_rect); let resize_y = self.side.opposite().side_axe(panel_sizer.panel_rect);
( (
@@ -764,27 +764,27 @@ impl Panel {
fn get_cursor_icon(&self, panel_sizer: &PanelSizer<'_>) -> CursorIcon { fn get_cursor_icon(&self, panel_sizer: &PanelSizer<'_>) -> CursorIcon {
if panel_sizer.size <= self.size_range.min { if panel_sizer.size <= self.size_range.min {
match self.side { match self.side {
Side::Vertical(v_side) => match v_side { PanelSide::Vertical(v_side) => match v_side {
VerticalSide::Left => CursorIcon::ResizeEast, VerticalSide::Left => CursorIcon::ResizeEast,
VerticalSide::Right => CursorIcon::ResizeWest, VerticalSide::Right => CursorIcon::ResizeWest,
}, },
Side::Horizontal(h_side) => match h_side { PanelSide::Horizontal(h_side) => match h_side {
HorizontalSide::Top => CursorIcon::ResizeSouth, HorizontalSide::Top => CursorIcon::ResizeSouth,
HorizontalSide::Bottom => CursorIcon::ResizeNorth, HorizontalSide::Bottom => CursorIcon::ResizeNorth,
}, },
} }
} else if panel_sizer.size < self.size_range.max { } else if panel_sizer.size < self.size_range.max {
match self.side { match self.side {
Side::Vertical(_) => CursorIcon::ResizeHorizontal, PanelSide::Vertical(_) => CursorIcon::ResizeHorizontal,
Side::Horizontal(_) => CursorIcon::ResizeVertical, PanelSide::Horizontal(_) => CursorIcon::ResizeVertical,
} }
} else { } else {
match self.side { match self.side {
Side::Vertical(v_side) => match v_side { PanelSide::Vertical(v_side) => match v_side {
VerticalSide::Left => CursorIcon::ResizeWest, VerticalSide::Left => CursorIcon::ResizeWest,
VerticalSide::Right => CursorIcon::ResizeEast, VerticalSide::Right => CursorIcon::ResizeEast,
}, },
Side::Horizontal(h_side) => match h_side { PanelSide::Horizontal(h_side) => match h_side {
HorizontalSide::Top => CursorIcon::ResizeNorth, HorizontalSide::Top => CursorIcon::ResizeNorth,
HorizontalSide::Bottom => CursorIcon::ResizeSouth, HorizontalSide::Bottom => CursorIcon::ResizeSouth,
}, },
@@ -848,13 +848,13 @@ impl Panel {
fn get_animated_size(ctx: &Context, panel: &Panel) -> f32 { fn get_animated_size(ctx: &Context, panel: &Panel) -> f32 {
let get_rect_state_size = |state: PanelState| match panel.side { let get_rect_state_size = |state: PanelState| match panel.side {
Side::Vertical(_) => state.rect.width(), PanelSide::Vertical(_) => state.rect.width(),
Side::Horizontal(_) => state.rect.height(), PanelSide::Horizontal(_) => state.rect.height(),
}; };
let get_spacing_size = || match panel.side { let get_spacing_size = || match panel.side {
Side::Vertical(_) => ctx.style().spacing.interact_size.x, PanelSide::Vertical(_) => ctx.style().spacing.interact_size.x,
Side::Horizontal(_) => ctx.style().spacing.interact_size.y, PanelSide::Horizontal(_) => ctx.style().spacing.interact_size.y,
}; };
PanelState::load(ctx, panel.id) PanelState::load(ctx, panel.id)

View File

@@ -1,6 +1,6 @@
use egui::emath::Rot2; use egui::emath::Rot2;
use egui::panel::HorizontalSide; use egui::panel::HorizontalSide;
use egui::panel::Side; use egui::panel::PanelSide;
use egui::panel::VerticalSide; use egui::panel::VerticalSide;
use egui::ImageFit; use egui::ImageFit;
use egui::Slider; use egui::Slider;
@@ -53,7 +53,7 @@ impl Default for ImageViewer {
impl eframe::App for ImageViewer { impl eframe::App for ImageViewer {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
egui::Panel::new(Side::Horizontal(HorizontalSide::Top), "url bar").show( egui::Panel::new(PanelSide::Horizontal(HorizontalSide::Top), "url bar").show(
ctx, ctx,
|ui| { |ui| {
ui.horizontal_centered(|ui| { ui.horizontal_centered(|ui| {
@@ -77,7 +77,7 @@ impl eframe::App for ImageViewer {
}, },
); );
egui::Panel::new(Side::Vertical(VerticalSide::Left), "controls").show(ctx, |ui| { egui::Panel::new(PanelSide::Vertical(VerticalSide::Left), "controls").show(ctx, |ui| {
// uv // uv
ui.label("UV"); ui.label("UV");
ui.add(Slider::new(&mut self.image_options.uv.min.x, 0.0..=1.0).text("min x")); ui.add(Slider::new(&mut self.image_options.uv.min.x, 0.0..=1.0).text("min x"));