refactoring: reduce duplicated code for default drawing of WidgetGroup

This commit is contained in:
Vadim Lopatin 2015-01-14 16:16:05 +03:00
parent e7cd6a2c9d
commit eba9fdfb79
6 changed files with 37 additions and 111 deletions

View File

@ -473,7 +473,7 @@ class FilePathPanelItem : HorizontalLayout {
}
/// Panel with buttons - path segments - for fast navigation to subdirs.
class FilePathPanelButtons : WidgetGroup {
class FilePathPanelButtons : WidgetGroupDefaultDrawing {
protected string _path;
Listener!OnPathSelectionHandler onPathSelectionListener;
protected bool onPathSelected(string path) {
@ -583,22 +583,6 @@ class FilePathPanelButtons : WidgetGroup {
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
}

View File

@ -472,7 +472,7 @@ class ResizerWidget : Widget {
/// Arranges items either vertically or horizontally
class LinearLayout : WidgetGroup {
class LinearLayout : WidgetGroupDefaultDrawing {
protected Orientation _orientation = Orientation.Vertical;
/// returns linear layout orientation (Vertical, Horizontal)
@property Orientation orientation() { return _orientation; }
@ -519,22 +519,6 @@ class LinearLayout : WidgetGroup {
applyPadding(rc);
_layoutItems.layout(rc);
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
}
@ -565,7 +549,7 @@ class HorizontalLayout : LinearLayout {
}
/// place all children into same place (usually, only one child should be visible at a time)
class FrameLayout : WidgetGroup {
class FrameLayout : WidgetGroupDefaultDrawing {
/// empty parameter list constructor - for usage by factory
this() {
this(null);
@ -617,23 +601,6 @@ class FrameLayout : WidgetGroup {
}
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
/// make one of children (with specified ID) visible, for the rest, set visibility to otherChildrenVisibility
bool showChild(string ID, Visibility otherChildrenVisibility = Visibility.Invisible, bool updateFocus = false) {
bool found = false;
@ -655,7 +622,7 @@ class FrameLayout : WidgetGroup {
}
/// layout children as table with rows and columns
class TableLayout : WidgetGroup {
class TableLayout : WidgetGroupDefaultDrawing {
this(string ID = null) {
super(ID);
@ -845,21 +812,4 @@ class TableLayout : WidgetGroup {
_cells.layout(rc);
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
}

View File

@ -206,7 +206,7 @@ class MenuItem {
}
/// widget to draw menu item
class MenuItemWidget : WidgetGroup {
class MenuItemWidget : WidgetGroupDefaultDrawing {
protected bool _mainMenu;
protected MenuItem _item;
protected ImageWidget _icon;
@ -298,24 +298,6 @@ class MenuItemWidget : WidgetGroup {
resetState(State.Checked);
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
updateState();
auto saver = ClipRectSaver(buf, rc, alpha);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
this(MenuItem item, bool mainMenu) {
id="menuitem";
_mainMenu = mainMenu;

View File

@ -300,9 +300,9 @@ class ScrollWidgetBase : WidgetGroup, OnScrollHandler {
If size of content widget exceeds available space, allows to scroll it.
*/
class ScrollWidget : ScrollWidgetBase {
protected WidgetGroup _contentWidget;
@property WidgetGroup contentWidget() { return _contentWidget; }
@property ScrollWidget contentWidget(WidgetGroup newContent) {
protected Widget _contentWidget;
@property Widget contentWidget() { return _contentWidget; }
@property ScrollWidget contentWidget(Widget newContent) {
if (_contentWidget) {
removeChild(childIndex(_contentWidget));
destroy(_contentWidget);

View File

@ -167,7 +167,7 @@ class TabItemList {
}
/// tab header - tab labels, with optional More button
class TabControl : WidgetGroup {
class TabControl : WidgetGroupDefaultDrawing {
protected TabItemList _items;
protected ImageButton _moreButton;
protected bool _enableCloseButton;
@ -338,24 +338,6 @@ class TabControl : WidgetGroup {
}
//Log.d("tabControl.layout exit");
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
//Log.d("tabControl.onDraw enter");
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
//Log.d("tabControl.onDraw exit");
}
protected string _selectedTabId;

View File

@ -1336,6 +1336,34 @@ class WidgetGroup : Widget {
}
/** WidgetGroup with default drawing of children (just draw all children) */
class WidgetGroupDefaultDrawing : WidgetGroup {
/// empty parameter list constructor - for usage by factory
this() {
this(null);
}
/// create with ID parameter
this(string ID) {
super(ID);
}
/// Draw widget at its position to buffer
override void onDraw(DrawBuf buf) {
if (visibility != Visibility.Visible)
return;
super.onDraw(buf);
Rect rc = _pos;
applyMargins(rc);
applyPadding(rc);
auto saver = ClipRectSaver(buf, rc);
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility != Visibility.Visible)
continue;
item.onDraw(buf);
}
}
}
immutable long ONE_SECOND = 10000000L;
/// Helper to handle animation progress