WindowServer+LibGUI+Taskbar: Store window progress as Optional<int>

We were previously using the magical constant -1 to signify that a
window had no progress state. Be more explicit an use an Optional. :^)
This commit is contained in:
Andreas Kling 2021-05-02 10:42:25 +02:00
parent 8af7cda17a
commit cc6db526a6
Notes: sideshowbarker 2024-07-18 18:47:29 +09:00
9 changed files with 20 additions and 21 deletions

View file

@ -126,7 +126,7 @@ public:
class WMWindowStateChangedEvent : public WMEvent {
public:
WMWindowStateChangedEvent(int client_id, int window_id, int parent_client_id, int parent_window_id, const StringView& title, const Gfx::IntRect& rect, bool is_active, bool is_modal, WindowType window_type, bool is_minimized, bool is_frameless, int progress)
WMWindowStateChangedEvent(int client_id, int window_id, int parent_client_id, int parent_window_id, const StringView& title, const Gfx::IntRect& rect, bool is_active, bool is_modal, WindowType window_type, bool is_minimized, bool is_frameless, Optional<int> progress)
: WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
, m_parent_client_id(parent_client_id)
, m_parent_window_id(parent_window_id)
@ -150,7 +150,7 @@ public:
WindowType window_type() const { return m_window_type; }
bool is_minimized() const { return m_minimized; }
bool is_frameless() const { return m_frameless; }
int progress() const { return m_progress; }
Optional<int> progress() const { return m_progress; }
private:
int m_parent_client_id;
@ -162,7 +162,7 @@ private:
bool m_modal;
bool m_minimized;
bool m_frameless;
int m_progress;
Optional<int> m_progress;
};
class WMWindowRectChangedEvent : public WMEvent {

View file

@ -1021,7 +1021,7 @@ void Window::did_remove_widget(Badge<Widget>, Widget& widget)
m_automatic_cursor_tracking_widget = nullptr;
}
void Window::set_progress(int progress)
void Window::set_progress(Optional<int> progress)
{
VERIFY(m_window_id);
WindowServerConnection::the().post_message(Messages::WindowServer::SetWindowProgress(m_window_id, progress));

View file

@ -190,7 +190,7 @@ public:
Window* find_parent_window();
void set_progress(int);
void set_progress(Optional<int>);
void update_cursor(Badge<Widget>) { update_cursor(); }

View file

@ -103,8 +103,6 @@ void TaskbarButton::paint_event(GUI::PaintEvent& event)
if (text().is_empty())
return;
bool has_progress = window.progress() >= 0 && window.progress() <= 100;
auto content_rect = rect().shrunken(8, 2);
auto icon_location = content_rect.center().translated(-(icon.width() / 2), -(icon.height() / 2));
if (!text().is_empty())
@ -125,12 +123,12 @@ void TaskbarButton::paint_event(GUI::PaintEvent& event)
icon_location.move_by(1, 1);
}
if (has_progress) {
if (window.progress().has_value()) {
auto adjusted_rect = rect().shrunken(4, 4);
if (is_being_pressed() || is_checked()) {
adjusted_rect.set_height(adjusted_rect.height() + 1);
}
paint_custom_progressbar(painter, adjusted_rect, text_rect, palette(), 0, 100, window.progress(), text(), font, text_alignment());
paint_custom_progressbar(painter, adjusted_rect, text_rect, palette(), 0, 100, window.progress().value(), text(), font, text_alignment());
}
if (is_enabled()) {
@ -142,6 +140,6 @@ void TaskbarButton::paint_event(GUI::PaintEvent& event)
painter.blit_disabled(icon_location, icon, icon.rect(), palette());
}
if (!has_progress)
if (!window.progress().has_value())
paint_text(painter, text_rect, font, text_alignment());
}

View file

@ -48,7 +48,7 @@ public:
void set_modal(bool modal) { m_modal = modal; }
bool is_modal() const { return m_modal; }
void set_progress(int progress)
void set_progress(Optional<int> progress)
{
if (m_progress == progress)
return;
@ -57,7 +57,7 @@ public:
m_button->update();
}
int progress() const { return m_progress; }
Optional<int> progress() const { return m_progress; }
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
@ -71,7 +71,7 @@ private:
bool m_active { false };
bool m_minimized { false };
bool m_modal { false };
int m_progress { -1 };
Optional<int> m_progress;
};
class WindowList {

View file

@ -918,7 +918,7 @@ bool Window::is_modal() const
return true;
}
void Window::set_progress(int progress)
void Window::set_progress(Optional<int> progress)
{
if (m_progress == progress)
return;

View file

@ -72,8 +72,6 @@ class Window final : public Core::Object
, public InlineLinkedListNode<Window> {
C_OBJECT(Window)
public:
Window(ClientConnection&, WindowType, int window_id, bool modal, bool minimizable, bool frameless, bool resizable, bool fullscreen, bool accessory, Window* parent_window = nullptr);
Window(Core::Object&, WindowType);
virtual ~Window() override;
bool is_modified() const { return m_modified; }
@ -295,8 +293,8 @@ public:
bool should_show_menubar() const { return m_should_show_menubar; }
int progress() const { return m_progress; }
void set_progress(int);
Optional<int> progress() const { return m_progress; }
void set_progress(Optional<int>);
bool is_destroyed() const { return m_destroyed; }
void destroy();
@ -324,6 +322,9 @@ public:
void set_menubar(Menubar*);
private:
Window(ClientConnection&, WindowType, int window_id, bool modal, bool minimizable, bool frameless, bool resizable, bool fullscreen, bool accessory, Window* parent_window = nullptr);
Window(Core::Object&, WindowType);
virtual void event(Core::Event&) override;
void handle_mouse_event(const MouseEvent&);
void handle_keydown_event(const KeyEvent&);
@ -398,7 +399,7 @@ private:
MenuItem* m_window_menu_close_item { nullptr };
MenuItem* m_window_menu_menubar_visibility_item { nullptr };
int m_minimize_animation_step { -1 };
int m_progress { -1 };
Optional<int> m_progress;
bool m_should_show_menubar { true };
bool m_modified { false };
};

View file

@ -1,7 +1,7 @@
endpoint WindowManagerClient
{
WindowRemoved(i32 wm_id, i32 client_id, i32 window_id) =|
WindowStateChanged(i32 wm_id, i32 client_id, i32 window_id, i32 parent_client_id, i32 parent_window_id, bool is_active, bool is_minimized, bool is_modal, bool is_frameless, i32 window_type, [UTF8] String title, Gfx::IntRect rect, i32 progress) =|
WindowStateChanged(i32 wm_id, i32 client_id, i32 window_id, i32 parent_client_id, i32 parent_window_id, bool is_active, bool is_minimized, bool is_modal, bool is_frameless, i32 window_type, [UTF8] String title, Gfx::IntRect rect, Optional<i32> progress) =|
WindowIconBitmapChanged(i32 wm_id, i32 client_id, i32 window_id, Gfx::ShareableBitmap bitmap) =|
WindowRectChanged(i32 wm_id, i32 client_id, i32 window_id, Gfx::IntRect rect) =|
AppletAreaSizeChanged(i32 wm_id, Gfx::IntSize size) =|

View file

@ -54,7 +54,7 @@ endpoint WindowServer
SetWindowTitle(i32 window_id, [UTF8] String title) => ()
GetWindowTitle(i32 window_id) => ([UTF8] String title)
SetWindowProgress(i32 window_id, i32 progress) =|
SetWindowProgress(i32 window_id, Optional<i32> progress) =|
SetWindowModified(i32 window_id, bool modified) =|
IsWindowModified(i32 window_id) => (bool modified)