1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Replace eframe::Frame commands and WindowInfo with egui (#3564)

* Part of https://github.com/emilk/egui/issues/3556

## In short
You now almost never need to use `eframe::Frame` - instead use
`ui.input(|i| i.viewport())` for information about the current viewport
(native window), and use `ctx.send_viewport_cmd` to modify it.

## In detail

This PR removes most commands from `eframe::Frame`, and replaces them
with `ViewportCommand`.
So `frame.close()` becomes
`ctx.send_viewport_cmd(ViewportCommand::Close)`, etc.

`frame.info().window_info` is now also gone, replaced with `ui.input(|i|
i.viewport())`.

`frame.info().native_pixels_per_point` is replaced with `ui.input(|i|
i.raw.native_pixels_per_point)`.

`RawInput` now contains one `ViewportInfo` for each viewport.

Screenshots are taken with
`ctx.send_viewport_cmd(ViewportCommand::Screenshots)` and are returned
in `egui::Event` which you can check with:

``` ust
ui.input(|i| {
    for event in &i.raw.events {
        if let egui::Event::Screenshot { viewport_id, image } = event {
            // handle it here
        }
    }
});
```

### Motivation
You no longer need to pass around the `&eframe::Frame` everywhere.
This also opens the door for other integrations to use the same API of
`ViewportCommand`s.
This commit is contained in:
Emil Ernerfeldt
2023-11-18 19:27:53 +01:00
committed by GitHub
parent 3e37e9dfc7
commit 1571027556
29 changed files with 905 additions and 915 deletions

View File

@@ -280,6 +280,42 @@ impl Sub<Vec2> for Pos2 {
}
}
impl Mul<f32> for Pos2 {
type Output = Pos2;
#[inline(always)]
fn mul(self, factor: f32) -> Pos2 {
Pos2 {
x: self.x * factor,
y: self.y * factor,
}
}
}
impl Mul<Pos2> for f32 {
type Output = Pos2;
#[inline(always)]
fn mul(self, vec: Pos2) -> Pos2 {
Pos2 {
x: self * vec.x,
y: self * vec.y,
}
}
}
impl Div<f32> for Pos2 {
type Output = Pos2;
#[inline(always)]
fn div(self, factor: f32) -> Pos2 {
Pos2 {
x: self.x / factor,
y: self.y / factor,
}
}
}
impl std::fmt::Debug for Pos2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{:.1} {:.1}]", self.x, self.y)

View File

@@ -622,3 +622,39 @@ impl From<[Pos2; 2]> for Rect {
Self { min, max }
}
}
impl Mul<f32> for Rect {
type Output = Rect;
#[inline]
fn mul(self, factor: f32) -> Rect {
Rect {
min: self.min * factor,
max: self.max * factor,
}
}
}
impl Mul<Rect> for f32 {
type Output = Rect;
#[inline]
fn mul(self, vec: Rect) -> Rect {
Rect {
min: self * vec.min,
max: self * vec.max,
}
}
}
impl Div<f32> for Rect {
type Output = Rect;
#[inline]
fn div(self, factor: f32) -> Rect {
Rect {
min: self.min / factor,
max: self.max / factor,
}
}
}