emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
22.47k stars 1.6k forks source link

Make some Memory methods public for Widget makers. #5044

Closed SymmetricChaos closed 2 months ago

SymmetricChaos commented 2 months ago

I've found while trying to make a DragValue based Widget that some methods used to create it are not public and so can't be used when designing a Widget. I think my workaround is causing some weird behavior because of this. The methods give some information but don't allow change of state so I assume there is no risk of misuse. Though perhaps they're private because there's an issue I don't know of.

My proposed solution is to change these lines in the definition of the Memory struct

    pub(crate) fn had_focus_last_frame(&self, id: Id) -> bool {
        self.focus().and_then(|f| f.id_previous_frame) == Some(id)
    }

    /// True if the given widget had keyboard focus last frame, but not this one.
    pub(crate) fn lost_focus(&self, id: Id) -> bool {
        self.had_focus_last_frame(id) && !self.has_focus(id)
    }

    /// True if the given widget has keyboard focus this frame, but didn't last frame.
    pub(crate) fn gained_focus(&self, id: Id) -> bool {
        !self.had_focus_last_frame(id) && self.has_focus(id)
    }

to these lines

    pub fn had_focus_last_frame(&self, id: Id) -> bool {
        self.focus().and_then(|f| f.id_previous_frame) == Some(id)
    }

    /// True if the given widget had keyboard focus last frame, but not this one.
    pub fn lost_focus(&self, id: Id) -> bool {
        self.had_focus_last_frame(id) && !self.has_focus(id)
    }

    /// True if the given widget has keyboard focus this frame, but didn't last frame.
    pub fn gained_focus(&self, id: Id) -> bool {
        !self.had_focus_last_frame(id) && self.has_focus(id)
    }

(I guess this could be a PR but I don't know how to do that)