genode/repos/gems/src/app/menu_view/button_widget.h

173 lines
4.3 KiB
C
Raw Normal View History

/*
* \brief Widget that handles hovered/selected state and hosts a child widget
* \author Norman Feske
* \date 2009-09-11
*/
/*
* Copyright (C) 2014-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _BUTTON_WIDGET_H_
#define _BUTTON_WIDGET_H_
/* demo includes */
#include <scout_gfx/icon_painter.h>
#include <util/lazy_value.h>
/* local includes */
#include "widget.h"
#include "scratch_surface.h"
namespace Menu_view { struct Button_widget; }
struct Menu_view::Button_widget : Widget, Animator::Item
{
bool hovered = false;
bool selected = false;
Texture<Pixel_rgb888> const * default_texture = nullptr;
Texture<Pixel_rgb888> const * hovered_texture = nullptr;
Lazy_value<int> blend { };
Padding padding { 9, 9, 2, 1 };
Area _space() const
{
return Area(margin.horizontal() + padding.horizontal(),
margin.vertical() + padding.vertical());
}
static bool _enabled(Xml_node node, char const *attr)
{
return node.attribute_value(attr, false);
}
Button_widget(Widget_factory &factory, Xml_node node, Unique_id unique_id)
:
Widget(factory, node, unique_id), Animator::Item(factory.animator)
{
margin = { 4, 4, 4, 4 };
}
void update(Xml_node node) override
{
bool const new_hovered = _enabled(node, "hovered");
bool const new_selected = _enabled(node, "selected");
if (new_selected) {
default_texture = _factory.styles.texture(node, "selected");
hovered_texture = _factory.styles.texture(node, "hselected");
} else {
default_texture = _factory.styles.texture(node, "default");
hovered_texture = _factory.styles.texture(node, "hovered");
}
if (new_hovered != hovered) {
if (new_hovered) {
blend.dst(255 << 8, 3);
} else {
blend.dst(0, 20);
}
animated(blend != blend.dst());
}
hovered = new_hovered;
selected = new_selected;
2017-08-09 19:52:19 +02:00
_update_children(node);
_children.for_each([&] (Widget &child) {
child.geometry(Rect(Point(margin.left + padding.left,
margin.top + padding.top),
child.min_size())); });
}
Area min_size() const override
{
/* determine minimum child size */
Area child_min_size(300, 10);
_children.for_each([&] (Widget const &child) {
child_min_size = child.min_size(); });
/* don't get smaller than the background texture */
Area const texture_size = default_texture->size();
return Area(max(_space().w() + child_min_size.w(), texture_size.w()),
max(_space().h() + child_min_size.h(), texture_size.h()));
}
void draw(Surface<Pixel_rgb888> &pixel_surface,
Surface<Pixel_alpha8> &alpha_surface,
Point at) const override
{
static Scratch_surface<Pixel_rgb888> scratch(_factory.alloc);
Area const texture_size = default_texture->size();
Rect const texture_rect(Point(0, 0), texture_size);
/*
* Mix from_texture and to_texture according to the blend value
*/
scratch.reset(texture_size);
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
scratch.apply([&] (Surface<Pixel_rgb888> &pixel, Surface<Pixel_alpha8> &alpha) {
Icon_painter::paint(pixel, texture_rect, *default_texture, 255);
Icon_painter::paint(alpha, texture_rect, *default_texture, 255);
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Icon_painter::paint(pixel, texture_rect, *hovered_texture, blend >> 8);
Icon_painter::paint(alpha, texture_rect, *hovered_texture, blend >> 8);
});
/*
* Apply blended texture to target surface
*/
2017-08-17 17:11:38 +02:00
Icon_painter::paint(pixel_surface, Rect(at, _animated_geometry.area()),
scratch.texture(), 255);
2017-08-17 17:11:38 +02:00
Icon_painter::paint(alpha_surface, Rect(at, _animated_geometry.area()),
scratch.texture(), 255);
if (selected)
at = at + Point(0, 1);
_draw_children(pixel_surface, alpha_surface, at);
}
void _layout() override
{
_children.for_each([&] (Widget &w) {
w.size(Area(geometry().w() - _space().w(),
geometry().h() - _space().h())); });
}
/******************************
** Animator::Item interface **
******************************/
void animate() override
{
blend.animate();
animated(blend != blend.dst());
}
private:
/**
* Noncopyable
*/
Button_widget(Button_widget const &);
Button_widget &operator = (Button_widget const &);
};
#endif /* _BUTTON_WIDGET_H_ */