theme: Possibility to specify default screen/ui scaling

This commit is contained in:
David Capello 2017-08-18 19:23:23 -03:00
parent a9c1b26db3
commit d00db433fe
5 changed files with 78 additions and 25 deletions

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<theme name="Default">
<theme name="Default"
screenscaling="2"
uiscaling="1">
<authors>
<author name="David Capello" url="http://davidcapello.com/" />
<author name="Ilija Melentijevic" url="http://ilkke.blogspot.com/" />

View File

@ -239,14 +239,7 @@ public:
gridScope()->Change.connect(base::Bind<void>(&OptionsWindow::onChangeGridScope, this));
}
// Screen/UI Scale
screenScale()->setSelectedItemIndex(
screenScale()->findItemIndexByValue(
base::convert_to<std::string>(m_pref.general.screenScale())));
uiScale()->setSelectedItemIndex(
uiScale()->findItemIndexByValue(
base::convert_to<std::string>(m_pref.general.uiScale())));
selectScalingItems();
if ((int(she::instance()->capabilities()) &
int(she::Capabilities::GpuAccelerationSwitch)) == int(she::Capabilities::GpuAccelerationSwitch)) {
@ -444,16 +437,30 @@ public:
"||&OK", warnings.c_str());
}
if (reset_screen) {
ui::Manager* manager = ui::Manager::getDefault();
she::Display* display = manager->getDisplay();
she::instance()->setGpuAcceleration(newGpuAccel);
display->setScale(newScreenScale);
manager->setDisplay(display);
}
if (reset_screen)
updateScreenScaling();
}
private:
void selectScalingItems() {
// Screen/UI Scale
screenScale()->setSelectedItemIndex(
screenScale()->findItemIndexByValue(
base::convert_to<std::string>(m_pref.general.screenScale())));
uiScale()->setSelectedItemIndex(
uiScale()->findItemIndexByValue(
base::convert_to<std::string>(m_pref.general.uiScale())));
}
void updateScreenScaling() {
ui::Manager* manager = ui::Manager::getDefault();
she::Display* display = manager->getDisplay();
she::instance()->setGpuAcceleration(m_pref.general.gpuAcceleration());
display->setScale(m_pref.general.screenScale());
manager->setDisplay(display);
}
void onNativeCursorChange() {
bool state =
// If the platform supports native cursors...
@ -688,13 +695,34 @@ private:
}
void onSelectTheme() {
ThemeItem* item = dynamic_cast<ThemeItem*>(themeList()->getSelectedChild());
if (item &&
item->themeName() != m_pref.theme.selected()) {
m_pref.theme.selected(item->themeName());
try {
ThemeItem* item = dynamic_cast<ThemeItem*>(themeList()->getSelectedChild());
if (item &&
item->themeName() != m_pref.theme.selected()) {
auto theme = static_cast<skin::SkinTheme*>(ui::get_theme());
ui::set_theme(ui::get_theme(),
ui::guiscale());
// Change theme name from preferences
m_pref.theme.selected(item->themeName());
// Preferred UI Scaling factor by the theme
if (theme->preferredUIScaling() > 0)
m_pref.general.uiScale(theme->preferredUIScaling());
ui::set_theme(theme, m_pref.general.uiScale());
// Preferred Screen Scaling by the theme
const int newScreenScale = theme->preferredScreenScaling();
if (newScreenScale > 0 &&
newScreenScale != m_pref.general.screenScale()) {
m_pref.general.screenScale(newScreenScale);
updateScreenScaling();
}
selectScalingItems();
}
}
catch (const std::exception& ex) {
Console::showException(ex);
}
}
@ -781,7 +809,7 @@ private:
extensionsList()->selectChild(item);
extensionsList()->layout();
}
catch (std::exception& ex) {
catch (const std::exception& ex) {
Console::showException(ex);
}
}
@ -810,7 +838,7 @@ private:
item->uninstall();
deleteExtensionItem(item);
}
catch (std::exception& ex) {
catch (const std::exception& ex) {
Console::showException(ex);
}
}

View File

@ -184,6 +184,8 @@ SkinTheme::SkinTheme()
, m_standardCursors(ui::kCursorTypes, nullptr)
, m_defaultFont(nullptr)
, m_miniFont(nullptr)
, m_preferredScreenScaling(-1)
, m_preferredUIScaling(-1)
{
}
@ -310,6 +312,22 @@ void SkinTheme::loadXml()
XmlDocumentRef doc = open_xml(xml_filename);
TiXmlHandle handle(doc.get());
// Load Preferred scaling
m_preferredScreenScaling = -1;
m_preferredUIScaling = -1;
{
TiXmlElement* xmlTheme = handle
.FirstChild("theme").ToElement();
if (xmlTheme) {
const char* screenScaling = xmlTheme->Attribute("screenscaling");
const char* uiScaling = xmlTheme->Attribute("uiscaling");
if (screenScaling)
m_preferredScreenScaling = std::strtol(screenScaling, nullptr, 10);
if (uiScaling)
m_preferredUIScaling = std::strtol(uiScaling, nullptr, 10);
}
}
// Load fonts
{
TiXmlElement* xmlFont = handle

View File

@ -48,6 +48,8 @@ namespace app {
~SkinTheme();
const std::string& path() { return m_path; }
int preferredScreenScaling() { return m_preferredScreenScaling; }
int preferredUIScaling() { return m_preferredUIScaling; }
she::Font* getDefaultFont() const override { return m_defaultFont; }
she::Font* getWidgetFont(const ui::Widget* widget) const override;
@ -156,6 +158,8 @@ namespace app {
std::map<std::string, she::Font*> m_themeFonts;
she::Font* m_defaultFont;
she::Font* m_miniFont;
int m_preferredScreenScaling;
int m_preferredUIScaling;
};
} // namespace skin

View File

@ -1,5 +1,5 @@
// SHE library
// Copyright (C) 2012-2016 David Capello
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -100,6 +100,7 @@ int SkiaDisplay::scale() const
void SkiaDisplay::setScale(int scale)
{
ASSERT(scale > 0);
m_window.setScale(scale);
}