Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "WindowIdentifier.h"
30#include <AK/String.h>
31#include <AK/HashMap.h>
32#include <LibGUI/Button.h>
33#include <LibGfx/Rect.h>
34
35class Window {
36public:
37 explicit Window(const WindowIdentifier& identifier)
38 : m_identifier(identifier)
39 {
40 }
41
42 ~Window()
43 {
44 if (m_button)
45 m_button->remove_from_parent();
46 }
47
48 WindowIdentifier identifier() const { return m_identifier; }
49
50 String title() const { return m_title; }
51 void set_title(const String& title) { m_title = title; }
52
53 Gfx::Rect rect() const { return m_rect; }
54 void set_rect(const Gfx::Rect& rect) { m_rect = rect; }
55
56 GUI::Button* button() { return m_button; }
57 void set_button(GUI::Button* button) { m_button = button; }
58
59 void set_active(bool active) { m_active = active; }
60 bool is_active() const { return m_active; }
61
62 void set_minimized(bool minimized) { m_minimized = minimized; }
63 bool is_minimized() const { return m_minimized; }
64
65 const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
66
67private:
68 WindowIdentifier m_identifier;
69 String m_title;
70 Gfx::Rect m_rect;
71 RefPtr<GUI::Button> m_button;
72 RefPtr<Gfx::Bitmap> m_icon;
73 bool m_active { false };
74 bool m_minimized { false };
75};
76
77class WindowList {
78public:
79 static WindowList& the();
80
81 template<typename Callback>
82 void for_each_window(Callback callback)
83 {
84 for (auto& it : m_windows)
85 callback(*it.value);
86 }
87
88 Window* window(const WindowIdentifier&);
89 Window& ensure_window(const WindowIdentifier&);
90 void remove_window(const WindowIdentifier&);
91
92 Function<NonnullRefPtr<GUI::Button>(const WindowIdentifier&)> aid_create_button;
93
94private:
95 HashMap<WindowIdentifier, NonnullOwnPtr<Window>> m_windows;
96};