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

Move viewports stuff to crates/egui/src/viewports.rs

This commit is contained in:
Konkitoman
2023-08-07 19:02:18 +03:00
parent ee2f2a7986
commit d45fc14a13
12 changed files with 513 additions and 509 deletions

View File

@@ -9,7 +9,7 @@ use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
#[cfg(feature = "accesskit")]
use egui::accesskit;
use egui::{window::ViewportBuilder, Context, NumExt as _};
use egui::{Context, NumExt as _, ViewportBuilder};
#[cfg(feature = "accesskit")]
use egui_winit::accesskit_winit;
use egui_winit::{native_pixels_per_point, EventResponse, WindowSettings};

View File

@@ -3,7 +3,7 @@
use std::{sync::Arc, time::Instant};
use egui::{epaint::ahash::HashMap, mutex::RwLock, window::ViewportBuilder};
use egui::{epaint::ahash::HashMap, mutex::RwLock, ViewportBuilder};
use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
use winit::{
event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget},
@@ -420,7 +420,7 @@ mod glow_integration {
use std::sync::Arc;
use egui::{
epaint::ahash::HashMap, mutex::RwLock, window::ViewportBuilder, NumExt as _, ViewportRender,
epaint::ahash::HashMap, mutex::RwLock, NumExt as _, ViewportBuilder, ViewportRender,
};
use egui_winit::EventResponse;
use glutin::{

View File

@@ -16,7 +16,7 @@ pub use accesskit_winit;
pub use egui;
#[cfg(feature = "accesskit")]
use egui::accesskit;
use egui::{mutex::RwLock, window::ViewportCommand};
use egui::{mutex::RwLock, ViewportCommand};
pub use winit;
pub mod clipboard;
@@ -916,7 +916,7 @@ pub fn process_viewport_commands(
let win = window.read();
match command {
egui::window::ViewportCommand::Drag => {
egui::ViewportCommand::Drag => {
// if this is not checked on x11 the input will be permanently taken until the app is killed!
if let Some(focus) = focused {
if focus == viewport_id {
@@ -924,10 +924,10 @@ pub fn process_viewport_commands(
}
}
}
egui::window::ViewportCommand::InnerSize(width, height) => {
egui::ViewportCommand::InnerSize(width, height) => {
win.set_inner_size(PhysicalSize::new(width, height));
}
egui::window::ViewportCommand::Resize(top, bottom, right, left) => {
egui::ViewportCommand::Resize(top, bottom, right, left) => {
win.drag_resize_window(match (top, bottom, right, left) {
(true, false, false, false) => ResizeDirection::North,
(false, true, false, false) => ResizeDirection::South,

View File

@@ -1,4 +1,4 @@
use egui::window::ViewportBuilder;
use egui::ViewportBuilder;
/// Can be used to store native window settings (position and size).
#[derive(Clone, Copy, Debug)]

View File

@@ -2,156 +2,11 @@
use crate::collapsing_header::CollapsingState;
use crate::{widget_text::WidgetTextGalley, *};
use crate::{ViewportBuilder, ViewportCommand};
use epaint::*;
use super::*;
#[derive(Hash, PartialEq, Clone)]
pub struct ViewportBuilder {
pub title: String,
pub name: Option<String>,
pub position: Option<(i32, i32)>,
pub inner_size: Option<(u32, u32)>,
pub fullscreen: bool,
pub maximized: bool,
pub resizable: bool,
pub transparent: bool,
pub decorations: bool,
pub icon: Option<(u32, u32, Vec<u8>)>,
pub active: bool,
pub visible: bool,
pub title_hidden: bool,
pub titlebar_transparent: bool,
pub fullsize_content_view: bool,
pub min_inner_size: Option<(u32, u32)>,
pub max_inner_size: Option<(u32, u32)>,
pub drag_and_drop: bool,
pub close_button: bool,
}
impl Default for ViewportBuilder {
fn default() -> Self {
Self {
title: "Dummy EGUI Window".into(),
name: None,
position: None,
inner_size: Some((300, 100)),
fullscreen: false,
maximized: false,
resizable: true,
transparent: false,
decorations: true,
icon: None,
active: true,
visible: true,
title_hidden: false,
titlebar_transparent: false,
fullsize_content_view: false,
min_inner_size: None,
max_inner_size: None,
drag_and_drop: true,
close_button: true,
}
}
}
impl ViewportBuilder {
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.decorations = decorations;
self
}
pub fn with_fullscreen(mut self, fullscreen: bool) -> Self {
self.fullscreen = fullscreen;
self
}
pub fn with_maximized(mut self, maximized: bool) -> Self {
self.maximized = maximized;
self
}
pub fn with_resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
}
pub fn with_transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
pub fn with_window_icon(mut self, icon: Option<(u32, u32, Vec<u8>)>) -> Self {
self.icon = icon;
self
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn with_title_hidden(mut self, title_hidden: bool) -> Self {
self.title_hidden = title_hidden;
self
}
pub fn with_titlebar_transparent(mut self, value: bool) -> Self {
self.titlebar_transparent = value;
self
}
pub fn with_fullsize_content_view(mut self, value: bool) -> Self {
self.fullsize_content_view = value;
self
}
pub fn with_inner_size(mut self, value: (u32, u32)) -> Self {
self.inner_size = Some(value);
self
}
pub fn with_min_inner_size(mut self, value: (u32, u32)) -> Self {
self.min_inner_size = Some(value);
self
}
pub fn with_max_inner_size(mut self, value: (u32, u32)) -> Self {
self.max_inner_size = Some(value);
self
}
pub fn with_drag_and_drop(mut self, value: bool) -> Self {
self.drag_and_drop = value;
self
}
pub fn with_position(mut self, value: (i32, i32)) -> Self {
self.position = Some(value);
self
}
}
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ViewportCommand {
Drag,
InnerSize(u32, u32),
/// Top, Bottom, Right, Left
Resize(bool, bool, bool, bool),
}
/// Builder for a floating window which can be dragged, closed, collapsed, resized and scrolled (off by default).
///
/// You can customize:

View File

@@ -4,7 +4,7 @@ use std::sync::Arc;
use crate::{
animation_manager::AnimationManager, data::output::PlatformOutput, frame_state::FrameState,
input_state::*, layers::GraphicLayers, memory::Options, os::OperatingSystem,
output::FullOutput, util::IdTypeMap, window::ViewportCommand, TextureHandle, *,
output::FullOutput, util::IdTypeMap, TextureHandle, ViewportCommand, *,
};
use ahash::HashMap;
use epaint::{mutex::*, stats::*, text::Fonts, TessellationOptions, *};
@@ -377,11 +377,11 @@ impl ContextImpl {
}
impl ContextImpl {
fn get_viewport_id(&self) -> u64 {
pub(crate) fn get_viewport_id(&self) -> u64 {
self.frame_stack.last().cloned().unwrap_or_default().0
}
fn get_parent_viewport_id(&self) -> u64 {
pub(crate) fn get_parent_viewport_id(&self) -> u64 {
self.frame_stack.last().cloned().unwrap_or_default().1
}
}
@@ -2132,8 +2132,7 @@ impl Context {
}
}
use containers::window::ViewportBuilder;
/// # Windows
// Viewports
impl Context {
pub fn get_viewport_id(&self) -> u64 {
self.read(|ctx| ctx.get_viewport_id())

View File

@@ -2,9 +2,8 @@
use std::sync::Arc;
use crate::window::ViewportCommand;
use crate::Context;
use crate::{window::ViewportBuilder, WidgetType};
use crate::{ViewportBuilder, ViewportCommand, WidgetType};
/// What egui emits each frame from [`crate::Context::run`].
///

View File

@@ -322,6 +322,7 @@ mod sense;
pub mod style;
mod ui;
pub mod util;
pub mod viewport;
pub mod widget_text;
pub mod widgets;
@@ -371,6 +372,7 @@ pub use {
style::{FontSelection, Margin, Style, TextStyle, Visuals},
text::{Galley, TextFormat},
ui::Ui,
viewport::*,
widget_text::{RichText, WidgetText},
widgets::*,
};
@@ -546,9 +548,6 @@ pub enum WidgetType {
Other,
}
/// This is used to render a viewport
pub type ViewportRender = dyn Fn(&Context, u64, u64) + Sync + Send;
// ----------------------------------------------------------------------------
/// For use in tests; especially doctests.

152
crates/egui/src/viewport.rs Normal file
View File

@@ -0,0 +1,152 @@
use std::sync::Arc;
use crate::Context;
/// This is used to render a viewport
pub type ViewportRender = dyn Fn(&Context, u64, u64) + Sync + Send;
#[derive(Hash, PartialEq, Clone)]
pub struct ViewportBuilder {
pub title: String,
pub name: Option<String>,
pub position: Option<(i32, i32)>,
pub inner_size: Option<(u32, u32)>,
pub fullscreen: bool,
pub maximized: bool,
pub resizable: bool,
pub transparent: bool,
pub decorations: bool,
pub icon: Option<(u32, u32, Vec<u8>)>,
pub active: bool,
pub visible: bool,
pub title_hidden: bool,
pub titlebar_transparent: bool,
pub fullsize_content_view: bool,
pub min_inner_size: Option<(u32, u32)>,
pub max_inner_size: Option<(u32, u32)>,
pub drag_and_drop: bool,
pub close_button: bool,
}
impl Default for ViewportBuilder {
fn default() -> Self {
Self {
title: "Dummy EGUI Window".into(),
name: None,
position: None,
inner_size: Some((300, 100)),
fullscreen: false,
maximized: false,
resizable: true,
transparent: false,
decorations: true,
icon: None,
active: true,
visible: true,
title_hidden: false,
titlebar_transparent: false,
fullsize_content_view: false,
min_inner_size: None,
max_inner_size: None,
drag_and_drop: true,
close_button: true,
}
}
}
impl ViewportBuilder {
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.decorations = decorations;
self
}
pub fn with_fullscreen(mut self, fullscreen: bool) -> Self {
self.fullscreen = fullscreen;
self
}
pub fn with_maximized(mut self, maximized: bool) -> Self {
self.maximized = maximized;
self
}
pub fn with_resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
}
pub fn with_transparent(mut self, transparent: bool) -> Self {
self.transparent = transparent;
self
}
pub fn with_window_icon(mut self, icon: Option<(u32, u32, Vec<u8>)>) -> Self {
self.icon = icon;
self
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn with_title_hidden(mut self, title_hidden: bool) -> Self {
self.title_hidden = title_hidden;
self
}
pub fn with_titlebar_transparent(mut self, value: bool) -> Self {
self.titlebar_transparent = value;
self
}
pub fn with_fullsize_content_view(mut self, value: bool) -> Self {
self.fullsize_content_view = value;
self
}
pub fn with_inner_size(mut self, value: (u32, u32)) -> Self {
self.inner_size = Some(value);
self
}
pub fn with_min_inner_size(mut self, value: (u32, u32)) -> Self {
self.min_inner_size = Some(value);
self
}
pub fn with_max_inner_size(mut self, value: (u32, u32)) -> Self {
self.max_inner_size = Some(value);
self
}
pub fn with_drag_and_drop(mut self, value: bool) -> Self {
self.drag_and_drop = value;
self
}
pub fn with_position(mut self, value: (i32, i32)) -> Self {
self.position = Some(value);
self
}
}
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ViewportCommand {
Drag,
InnerSize(u32, u32),
/// Top, Bottom, Right, Left
Resize(bool, bool, bool, bool),
}