summaryrefslogtreecommitdiff
path: root/Widgets
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2018-10-13 20:59:25 +0200
committerAndreas Kling <awesomekling@gmail.com>2018-10-13 20:59:25 +0200
commit6f1b384cde73b4f701057f009a02f936aaf2b785 (patch)
tree1bb4ea857a1ce2352b06d5bfd4081a3e35f7c50c /Widgets
parent1929cb6b71452a00325f8098e5d998462a742a9f (diff)
downloadserenity-6f1b384cde73b4f701057f009a02f936aaf2b785.zip
More work on focus.
ListBox now renders differently depending on focus state.
Diffstat (limited to 'Widgets')
-rw-r--r--Widgets/ListBox.cpp13
-rw-r--r--Widgets/Painter.cpp9
-rw-r--r--Widgets/Painter.h2
-rw-r--r--Widgets/Widget.cpp12
-rw-r--r--Widgets/Window.cpp11
-rw-r--r--Widgets/Window.h1
6 files changed, 39 insertions, 9 deletions
diff --git a/Widgets/ListBox.cpp b/Widgets/ListBox.cpp
index 4f067e75f4..ab7306bfad 100644
--- a/Widgets/ListBox.cpp
+++ b/Widgets/ListBox.cpp
@@ -1,6 +1,7 @@
#include "ListBox.h"
#include "Painter.h"
#include "Font.h"
+#include "Window.h"
ListBox::ListBox(Widget* parent)
: Widget(parent)
@@ -19,16 +20,24 @@ unsigned ListBox::itemHeight() const
void ListBox::onPaint(PaintEvent&)
{
Painter painter(*this);
+
+ // FIXME: Reduce overdraw.
painter.fillRect(rect(), Color::White);
painter.drawRect(rect(), Color::Black);
+ if (isFocused())
+ painter.drawFocusRect(rect());
+
for (unsigned i = m_scrollOffset; i < m_items.size(); ++i) {
- Rect itemRect(1, 1 + (i * itemHeight()), width() - 2, itemHeight());
+ Rect itemRect(2, 2 + (i * itemHeight()), width() - 4, itemHeight());
Rect textRect(itemRect.x() + 1, itemRect.y() + 1, itemRect.width() - 2, itemRect.height() - 2);
Color itemTextColor = foregroundColor();
if (m_selectedIndex == i) {
- painter.fillRect(itemRect, Color(0, 32, 128));
+ if (isFocused())
+ painter.fillRect(itemRect, Color(0, 32, 128));
+ else
+ painter.fillRect(itemRect, Color(96, 96, 96));
itemTextColor = Color::White;
}
painter.drawText(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor);
diff --git a/Widgets/Painter.cpp b/Widgets/Painter.cpp
index 3fbe0993aa..8a5f4a1d00 100644
--- a/Widgets/Painter.cpp
+++ b/Widgets/Painter.cpp
@@ -201,3 +201,12 @@ void Painter::drawLine(const Point& p1, const Point& p2, Color color)
}
}
}
+
+void Painter::drawFocusRect(const Rect& rect)
+{
+ Rect focusRect = rect;
+ focusRect.moveBy(1, 1);
+ focusRect.setWidth(focusRect.width() - 2);
+ focusRect.setHeight(focusRect.height() - 2);
+ drawRect(focusRect, Color(96, 96, 192));
+}
diff --git a/Widgets/Painter.h b/Widgets/Painter.h
index 11b6aa7600..248887de7c 100644
--- a/Widgets/Painter.h
+++ b/Widgets/Painter.h
@@ -22,6 +22,8 @@ public:
void drawPixel(const Point&, Color);
void drawLine(const Point& p1, const Point& p2, Color);
+ void drawFocusRect(const Rect&);
+
void xorRect(const Rect&, Color);
const Font& font() const;
diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp
index d007fe9d3f..3436aa5c10 100644
--- a/Widgets/Widget.cpp
+++ b/Widgets/Widget.cpp
@@ -49,6 +49,10 @@ void Widget::event(Event& event)
case Event::MouseMove:
return onMouseMove(static_cast<MouseEvent&>(event));
case Event::MouseDown:
+ if (auto* win = window()) {
+ // FIXME: if (acceptsFocus())
+ win->setFocusedWidget(this);
+ }
return onMouseDown(static_cast<MouseEvent&>(event));
case Event::MouseUp:
return onMouseUp(static_cast<MouseEvent&>(event));
@@ -123,13 +127,15 @@ void Widget::setWindow(Window* window)
bool Widget::isFocused() const
{
- return m_window && m_window->focusedWidget() == this;
+ if (auto* win = window())
+ return win->isActive() && win->focusedWidget() == this;
+ return false;
}
void Widget::setFocus(bool focus)
{
if (focus == isFocused())
return;
- if (m_window)
- m_window->setFocusedWidget(this);
+ if (auto* win = window())
+ win->setFocusedWidget(this);
}
diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp
index 0926075bdb..f521b94c4e 100644
--- a/Widgets/Window.cpp
+++ b/Widgets/Window.cpp
@@ -56,8 +56,7 @@ void Window::event(Event& event)
//printf("hit test for %d,%d found: %s{%p} %d,%d\n", me.x(), me.y(), result.widget->className(), result.widget, result.localX, result.localY);
// FIXME: Re-use the existing event instead of crafting a new one?
auto localEvent = make<MouseEvent>(event.type(), result.localX, result.localY, me.button());
- result.widget->event(*localEvent);
- return m_mainWidget->event(event);
+ return result.widget->event(*localEvent);
}
return Object::event(event);
}
@@ -101,10 +100,14 @@ void Window::setFocusedWidget(Widget* widget)
{
if (m_focusedWidget.ptr() == widget)
return;
+ auto* previouslyFocusedWidget = m_focusedWidget.ptr();
if (!widget) {
m_focusedWidget = nullptr;
- return;
+ } else {
+ m_focusedWidget = widget->makeWeakPtr();
+ m_focusedWidget->repaint(Rect());
}
- m_focusedWidget = widget->makeWeakPtr();
+ if (previouslyFocusedWidget)
+ previouslyFocusedWidget->repaint(Rect());
}
diff --git a/Widgets/Window.h b/Widgets/Window.h
index eb94390b92..ceb7bd3433 100644
--- a/Widgets/Window.h
+++ b/Widgets/Window.h
@@ -41,6 +41,7 @@ public:
bool isActive() const;
Widget* focusedWidget() { return m_focusedWidget.ptr(); }
+ const Widget* focusedWidget() const { return m_focusedWidget.ptr(); }
void setFocusedWidget(Widget*);
private: