Rename Floating to Area

This commit is contained in:
Emil Ernerfeldt 2020-05-10 13:14:52 +02:00
parent 90020b41a8
commit fd99213222
6 changed files with 54 additions and 58 deletions

View file

@ -1,12 +1,12 @@
pub mod area;
pub mod collapsing_header;
pub mod floating;
pub mod frame;
pub mod resize;
pub mod scroll_area;
pub mod window;
pub use {
collapsing_header::CollapsingHeader, floating::Floating, frame::Frame, resize::Resize,
area::Area, collapsing_header::CollapsingHeader, frame::Frame, resize::Resize,
scroll_area::ScrollArea, window::Window,
};

View file

@ -1,7 +1,6 @@
//! A Floating is an Ui that has no parent, it floats on the background.
//! It is potentioally movable.
//! It has no frame or own size.
//! It is the foundation for a window
//! Area is a `Ui` that has no parent, it floats on the background.
//! It has no frame or own size. It is potentioally movable.
//! It is the foundation for windows and popups.
use std::{fmt::Debug, hash::Hash, sync::Arc};
@ -15,14 +14,14 @@ pub(crate) struct State {
/// Last know size. Used for catching clicks.
pub size: Vec2,
/// You can throw a Floating thing. It's fun.
/// You can throw a moveable Area. It's fun.
/// TODO: separate out moveable to container?
#[serde(skip)]
pub vel: Vec2,
}
// TODO: rename Floating to something else. Area?
#[derive(Clone, Copy, Debug)]
pub struct Floating {
pub struct Area {
id: Id,
movable: bool,
always_on_top: bool,
@ -30,7 +29,7 @@ pub struct Floating {
fixed_pos: Option<Pos2>,
}
impl Floating {
impl Area {
pub fn new(id_source: impl Hash) -> Self {
Self {
id: Id::new(id_source),
@ -46,7 +45,7 @@ impl Floating {
self
}
/// Always show as top Floating
/// Always show as top Area
pub fn always_on_top(mut self) -> Self {
self.always_on_top = true;
self
@ -65,14 +64,14 @@ impl Floating {
}
}
impl Floating {
impl Area {
// TODO
// pub fn show(self, ui: &Ui, add_contents: impl FnOnce(&mut Ui)) {
// let default_pos = self.default_pos.unwrap_or_else(|| ui.top_left() + pos2(100.0, 100.0)); // TODO
// }
pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) -> InteractInfo {
let Floating {
let Area {
id,
movable,
always_on_top,
@ -81,10 +80,10 @@ impl Floating {
} = self;
let default_pos = default_pos.unwrap_or_else(|| pos2(100.0, 100.0)); // TODO
let id = ctx.register_unique_id(id, "Floating", default_pos);
let id = ctx.register_unique_id(id, "Area", default_pos);
let layer = Layer::Window(id);
let (mut state, _is_new) = match ctx.memory().get_floating(id) {
let (mut state, _is_new) = match ctx.memory().get_area(id) {
Some(state) => (state, false),
None => {
let state = State {
@ -142,19 +141,19 @@ impl Floating {
// ctx.debug_rect(
// Rect::from_min_size(state.pos, state.size),
// &format!("Floating size: {:?}", state.size),
// &format!("Area size: {:?}", state.size),
// );
if move_interact.active || mouse_pressed_on_floating(ctx, id) || always_on_top {
ctx.memory().move_floating_to_top(id);
if move_interact.active || mouse_pressed_on_area(ctx, id) || always_on_top {
ctx.memory().move_area_to_top(id);
}
ctx.memory().set_floating_state(id, state);
ctx.memory().set_area_state(id, state);
move_interact
}
}
fn mouse_pressed_on_floating(ctx: &Context, id: Id) -> bool {
fn mouse_pressed_on_area(ctx: &Context, id: Id) -> bool {
if let Some(mouse_pos) = ctx.input().mouse_pos {
ctx.input().mouse_pressed && ctx.memory().layer_at(mouse_pos) == Layer::Window(id)
} else {

View file

@ -7,7 +7,7 @@ use super::*;
/// A wrapper around other containers for things you often want in a window
pub struct Window {
pub title_label: Label,
pub floating: Floating,
pub area: Area,
pub frame: Option<Frame>,
pub resize: Resize,
pub scroll: ScrollArea,
@ -17,13 +17,13 @@ impl Window {
// TODO: Into<Label>
pub fn new(title: impl Into<String>) -> Self {
let title = title.into();
let floating = Floating::new(&title);
let area = Area::new(&title);
let title_label = Label::new(title)
.text_style(TextStyle::Heading)
.multiline(false);
Self {
title_label,
floating,
area,
frame: None,
resize: Resize::default()
.handle_offset(Vec2::splat(4.0))
@ -52,7 +52,7 @@ impl Window {
}
pub fn default_pos(mut self, default_pos: Pos2) -> Self {
self.floating = self.floating.default_pos(default_pos);
self.area = self.area.default_pos(default_pos);
self
}
@ -88,7 +88,7 @@ impl Window {
pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) -> InteractInfo {
let Window {
title_label,
floating,
area,
frame,
resize,
scroll,
@ -96,7 +96,7 @@ impl Window {
let frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
// TODO: easier way to compose these
floating.show(ctx, |ui| {
area.show(ctx, |ui| {
frame.show(ui, |ui| {
resize.show(ui, |ui| {
ui.add(title_label);

View file

@ -182,7 +182,7 @@ impl Context {
fn drain_paint_lists(&self) -> Vec<(Rect, PaintCmd)> {
let memory = self.memory();
self.graphics().drain(memory.floating_order()).collect()
self.graphics().drain(memory.area_order()).collect()
}
fn paint(&self) -> PaintBatches {
@ -208,7 +208,7 @@ impl Context {
// ---------------------------------------------------------------------
/// An ui for the entire screen, behind any windows.
/// A `Ui` for the entire screen, behind any windows.
pub fn fullscreen_ui(self: &Arc<Self>) -> Ui {
let rect = Rect::from_min_size(Default::default(), self.input().screen_size);
Ui::new(self.clone(), Layer::Background, Id::background(), rect)

View file

@ -35,13 +35,10 @@ impl GraphicLayers {
}
}
pub fn drain(
&mut self,
window_order: &[Id],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
pub fn drain(&mut self, area_order: &[Id]) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = self.bg.drain(..).collect();
for id in window_order {
for id in area_order {
if let Some(window) = self.windows.get_mut(id) {
all_commands.extend(window.drain(..));
}

View file

@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use crate::{
containers::{collapsing_header, floating, resize, scroll_area},
containers::{area, collapsing_header, resize, scroll_area},
Id, Layer, Pos2, Rect,
};
@ -21,40 +21,40 @@ pub struct Memory {
pub(crate) scroll_areas: HashMap<Id, scroll_area::State>,
pub(crate) resize: HashMap<Id, resize::State>,
floating: HashMap<Id, floating::State>,
area: HashMap<Id, area::State>,
/// Top is last
floating_order: Vec<Id>,
floating_visible_last_frame: HashSet<Id>,
floating_visible_current_frame: HashSet<Id>,
area_order: Vec<Id>,
area_visible_last_frame: HashSet<Id>,
area_visible_current_frame: HashSet<Id>,
}
impl Memory {
pub(crate) fn get_floating(&mut self, id: Id) -> Option<floating::State> {
self.floating.get(&id).cloned()
pub(crate) fn get_area(&mut self, id: Id) -> Option<area::State> {
self.area.get(&id).cloned()
}
pub(crate) fn floating_order(&self) -> &[Id] {
&self.floating_order
pub(crate) fn area_order(&self) -> &[Id] {
&self.area_order
}
pub(crate) fn set_floating_state(&mut self, id: Id, state: floating::State) {
self.floating_visible_current_frame.insert(id);
let did_insert = self.floating.insert(id, state).is_none();
pub(crate) fn set_area_state(&mut self, id: Id, state: area::State) {
self.area_visible_current_frame.insert(id);
let did_insert = self.area.insert(id, state).is_none();
if did_insert {
self.floating_order.push(id);
self.area_order.push(id);
}
}
/// TODO: call once at the start of the frame for the current mouse pos
pub fn layer_at(&self, pos: Pos2) -> Layer {
for floating_id in self.floating_order.iter().rev() {
if self.floating_visible_last_frame.contains(floating_id)
|| self.floating_visible_current_frame.contains(floating_id)
for area_id in self.area_order.iter().rev() {
if self.area_visible_last_frame.contains(area_id)
|| self.area_visible_current_frame.contains(area_id)
{
if let Some(state) = self.floating.get(floating_id) {
if let Some(state) = self.area.get(area_id) {
let rect = Rect::from_min_size(state.pos, state.size);
if rect.contains(pos) {
return Layer::Window(*floating_id);
return Layer::Window(*area_id);
}
}
}
@ -62,18 +62,18 @@ impl Memory {
Layer::Background
}
pub fn move_floating_to_top(&mut self, id: Id) {
if self.floating_order.last() == Some(&id) {
pub fn move_area_to_top(&mut self, id: Id) {
if self.area_order.last() == Some(&id) {
return; // common case early-out
}
if let Some(index) = self.floating_order.iter().position(|x| *x == id) {
self.floating_order.remove(index);
if let Some(index) = self.area_order.iter().position(|x| *x == id) {
self.area_order.remove(index);
}
self.floating_order.push(id);
self.floating_visible_current_frame.insert(id);
self.area_order.push(id);
self.area_visible_current_frame.insert(id);
}
pub(crate) fn begin_frame(&mut self) {
self.floating_visible_last_frame = std::mem::take(&mut self.floating_visible_current_frame);
self.area_visible_last_frame = std::mem::take(&mut self.area_visible_current_frame);
}
}