New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Style struct #55
Add Style struct #55
Conversation
BREAKING CHANGE: existing apps that mutate member variables directly will need to be edited to either use ui::Stylesheet (preferred), or at the very least, mutate those variables in the `style` struct. Example, setting font_size: // The old way: myWidget->font_size = 32 // The new way (encouraged): const ui::Stylesheet MY_WIDGET_STYLE = ui::Stylesheet().font_size(32) myWidget->set_style(MY_WIDGET_STYLE) // The new way (discouraged, but still possible): myWidget->style.font_size = 32 Example, setting style defaults: // The old way ui::Text::DEFAULT_FS = 32 ui::Text::DEFAULT_JUSTIFY = ui::Text::JUSTIFY::CENTER // The new way ui::Style::DEFAULT.font_size = 32 ui::Style::DEFAULT.justify_center() ui::Style::DEFAULT.underline = true // not supported before now
@@ -65,13 +69,6 @@ namespace ui: | |||
input::SynMotionEvent fake | |||
self.on_mouse_click(fake) | |||
|
|||
// function: set_justification |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced by set_style()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is super cool, code looks great - i'll compile and test things out
// When adding a new style, make sure to also add builders in Stylesheet | ||
// and Stylesheet::Inherited. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if only there was easier metaprogramming :-(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, there's always macros :)
most things compile (remux, harmony, mines, sharenote, simple, etc) except for minesweeper:
but this made me think that you maybe had plans for valign (which would make these lines obsolete). i also tested out remux, harmony, mines, simple - everything looks good so far |
Woops -- sorry I was doing test compiles along the way, but that diff was a last-minute change and I must not have tried to compile it. Yep, I was thinking about having valign be the next PR.
Awesome, thanks for testing those. I think I made changes in the right places, but I don't know enough about the rest of the apps to know if something was broken visually. |
Latest commits should address c++17 syntax, the failed minesweeper build, and operator+= LMK if you'd prefer rebasing those into the previous commits instead of adding new ones. |
@@ -241,3 +242,7 @@ namespace ui: | |||
void switch_mode(int mode): | |||
// TODO | |||
pass | |||
|
|||
; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were a couple files where it looked like okp wasn't adding the semicolon after the last class. Maybe when there are multiple classes in the file?
return | ||
|
||
void set_style(const Stylesheet & style): | ||
Widget::set_style(style) | ||
self.textWidget->set_style(Stylesheet::Inherited(self.style).text_style()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.textWidget->set_style(Stylesheet::Inherited(self.style).text_style())
vs
self.textWidget->set_style(self.style.inherit().text_style())
thank you for the diff! |
Based on #50 and #51 this adds a
Style
struct to all widgets.I've left in the concept of defaults, but instead of those being individual static members of the style class (DEFAULT_FS, DEFAULT_JUSTIFY), there's now a default Style, so anything in the Style struct can have a default.
The style system involves three classes:
Style
-- a simple struct holding all style information.Stylesheet
-- used to update individual parts of aStyle
.Stylesheet::Inherited
-- aStylesheet
used to copy some variables from oneStyle
to another.The approach this PR suggests to managing styles is to create a
Stylesheet
and apply it to widgets as appropriate. Some widgets have default stylesheets (e.g. Button). For instance, suppose you have two different kinds of buttons you might want to support in your app. One with an outline and centered text, and the other with an underline and left-aligned text. This might look like:Alternate approaches
I started going down the road of creating a separate style hierarchy that would be used via multiple inheritance. That's a little more flexible, but less straightforward and IMO a little more confusing (as things always are w/ multiple inheritance). The advantage is mostly that you don't have to store every style in every class, since not all styles will be relevant in every class. There's probably a way to get something similar using has-a instead of is-a, but the solution wasn't immediately obvious to me. An example: