summaryrefslogtreecommitdiff
path: root/unity-shared
diff options
authorMarco Trevisan (Treviño) <mail@3v1n0.net>2014-07-28 23:26:54 +0200
committerMarco Trevisan (Treviño) <mail@3v1n0.net>2014-07-28 23:26:54 +0200
commit8a5ad42edd413de24641d904ee46804f60a179d6 (patch)
treef2a655382f5638e93ddbc230dc28d4e66dfecc05 /unity-shared
parent7960e599945ed4a8a4daf49d8a5311e55a8c7437 (diff)
TextInput: add scaling support
(bzr r3844.10.19)
Diffstat (limited to 'unity-shared')
-rw-r--r--unity-shared/TextInput.cpp138
-rw-r--r--unity-shared/TextInput.h4
2 files changed, 75 insertions, 67 deletions
diff --git a/unity-shared/TextInput.cpp b/unity-shared/TextInput.cpp
index 54da0ee0a..f93b8b8fe 100644
--- a/unity-shared/TextInput.cpp
+++ b/unity-shared/TextInput.cpp
@@ -44,13 +44,11 @@ namespace unity
namespace
{
-const float kExpandDefaultIconOpacity = 1.0f;
-
-const int SPACE_BETWEEN_ENTRY_AND_HIGHLIGHT = 10;
-const int LEFT_INTERNAL_PADDING = 6;
-const int TEXT_INPUT_RIGHT_BORDER = 10;
-
-const int HIGHLIGHT_HEIGHT = 24;
+const int BORDER_RADIUS = 5;
+const RawPixel SPACE_BETWEEN_ENTRY_AND_HIGHLIGHT = 10_em;
+const RawPixel LEFT_INTERNAL_PADDING = 6_em;
+const RawPixel TEXT_INPUT_RIGHT_BORDER = 10_em;
+const RawPixel HINT_PADDING = 3_em;
const RawPixel TOOLTIP_Y_OFFSET = 3_em;
const RawPixel TOOLTIP_OFFSET = 10_em;
@@ -65,7 +63,7 @@ const std::string HINT_LABEL_DEFAULT_FONT_NAME = "Ubuntu";
const int HINT_LABEL_FONT_SIZE = 11;
const std::string PANGO_ENTRY_DEFAULT_FONT_FAMILY = "Ubuntu";
-const int PANGO_ENTRY_FONT_SIZE = 14;
+const RawPixel PANGO_ENTRY_FONT_SIZE = 14_em;
}
@@ -73,7 +71,7 @@ nux::logging::Logger logger("unity.textinput");
NUX_IMPLEMENT_OBJECT_TYPE(TextInput);
-nux::AbstractPaintLayer* CreateWarningLayer(nux::BaseTexture* texture)
+std::shared_ptr<nux::AbstractPaintLayer> CreateWarningLayer(nux::BaseTexture* texture)
{
// Create the texture layer
nux::TexCoordXForm texxform;
@@ -89,11 +87,7 @@ nux::AbstractPaintLayer* CreateWarningLayer(nux::BaseTexture* texture)
rop.SrcBlend = GL_ONE;
rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;
- return (new nux::TextureLayer(texture->GetDeviceTexture(),
- texxform,
- nux::color::White,
- true,
- rop));
+ return std::make_shared<nux::TextureLayer>(texture->GetDeviceTexture(), texxform, nux::color::White, true, rop);
}
TextInput::TextInput(NUX_FILE_LINE_DECL)
@@ -103,30 +97,31 @@ TextInput::TextInput(NUX_FILE_LINE_DECL)
, hint_font_size(HINT_LABEL_FONT_SIZE)
, show_activator(false)
, show_caps_lock(false)
+ , scale(1.0)
, bg_layer_(new nux::ColorLayer(nux::Color(0xff595853), true))
, caps_lock_on(false)
, last_width_(-1)
, last_height_(-1)
- , mouse_over_warning_icon_(false)
{
layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
- layout_->SetLeftAndRightPadding(LEFT_INTERNAL_PADDING, TEXT_INPUT_RIGHT_BORDER);
- layout_->SetSpaceBetweenChildren(SPACE_BETWEEN_ENTRY_AND_HIGHLIGHT);
+ layout_->SetLeftAndRightPadding(LEFT_INTERNAL_PADDING.CP(scale), TEXT_INPUT_RIGHT_BORDER.CP(scale));
+ layout_->SetSpaceBetweenChildren(SPACE_BETWEEN_ENTRY_AND_HIGHLIGHT.CP(scale));
SetLayout(layout_);
- nux::HLayout* hint_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
- hint_layout->SetLeftAndRightPadding(3, 3);
+ hint_layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
+ hint_layout_->SetLeftAndRightPadding(HINT_PADDING.CP(scale), HINT_PADDING.CP(scale));
hint_ = new StaticCairoText("");
hint_->SetTextColor(nux::Color(1.0f, 1.0f, 1.0f, 0.5f));
- hint_layout->AddView(hint_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
+ hint_->SetScale(scale);
+ hint_layout_->AddView(hint_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
hint_font_name.changed.connect(sigc::hide(sigc::mem_fun(this, &TextInput::UpdateHintFont)));
hint_font_size.changed.connect(sigc::hide(sigc::mem_fun(this, &TextInput::UpdateHintFont)));
UpdateHintFont();
pango_entry_ = new IMTextEntry();
pango_entry_->SetFontFamily(PANGO_ENTRY_DEFAULT_FONT_FAMILY.c_str());
- pango_entry_->SetFontSize(PANGO_ENTRY_FONT_SIZE);
+ pango_entry_->SetFontSize(PANGO_ENTRY_FONT_SIZE.CP(scale));
pango_entry_->cursor_moved.connect([this](int i) { QueueDraw(); });
pango_entry_->mouse_down.connect(sigc::mem_fun(this, &TextInput::OnMouseButtonDown));
pango_entry_->key_up.connect(sigc::mem_fun(this, &TextInput::OnKeyUp));
@@ -136,14 +131,14 @@ TextInput::TextInput(NUX_FILE_LINE_DECL)
});
layered_layout_ = new nux::LayeredLayout();
- layered_layout_->AddLayer(hint_layout);
+ layered_layout_->AddLayer(hint_layout_);
layered_layout_->AddLayer(pango_entry_);
layered_layout_->SetPaintAll(true);
layered_layout_->SetActiveLayerN(1);
layout_->AddView(layered_layout_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FIX);
// Caps lock warning
- warning_ = new IconTexture(LoadWarningIcon(DEFAULT_ICON_SIZE));
+ warning_ = new IconTexture(LoadWarningIcon(DEFAULT_ICON_SIZE.CP(scale)));
warning_->SetVisible(caps_lock_on());
layout_->AddView(warning_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
caps_lock_on.changed.connect([this] (bool on) {
@@ -155,15 +150,11 @@ TextInput::TextInput(NUX_FILE_LINE_DECL)
}
});
- show_caps_lock.changed.connect([this] (bool changed) {
- if (!warning_tooltip_.IsValid())
- LoadWarningTooltip();
-
- CheckIfCapsLockOn();
- });
+ show_caps_lock.changed.connect(sigc::hide(sigc::mem_fun(this, &TextInput::CheckIfCapsLockOn)));
+ scale.changed.connect(sigc::mem_fun(this, &TextInput::UpdateScale));
// Activator
- activator_ = new IconTexture(LoadActivatorIcon(DEFAULT_ICON_SIZE));
+ activator_ = new IconTexture(LoadActivatorIcon(DEFAULT_ICON_SIZE.CP(scale)));
activator_->SetVisible(show_activator());
layout_->AddView(activator_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
@@ -178,12 +169,11 @@ TextInput::TextInput(NUX_FILE_LINE_DECL)
// Spinner
spinner_ = new SearchBarSpinner();
spinner_->SetVisible(false);
- spinner_->SetMinMaxSize(22, 22);
+ spinner_->scale = scale();
layout_->AddView(spinner_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
- sig_manager_.Add<void, GtkSettings*, GParamSpec*>(gtk_settings_get_default(),
- "notify::gtk-font-name", sigc::mem_fun(this, &TextInput::OnFontChanged));
- OnFontChanged(gtk_settings_get_default());
+ sig_manager_.Add<void, GtkSettings*>(gtk_settings_get_default(), "notify::gtk-font-name", sigc::hide(sigc::mem_fun(this, &TextInput::OnFontChanged)));
+ OnFontChanged();
input_string.SetGetterFunction(sigc::mem_fun(this, &TextInput::get_input_string));
input_string.SetSetterFunction(sigc::mem_fun(this, &TextInput::set_input_string));
@@ -192,16 +182,39 @@ TextInput::TextInput(NUX_FILE_LINE_DECL)
input_hint.changed.connect([this](std::string const& s) { OnInputHintChanged(); });
warning_->mouse_enter.connect([this] (int x, int y, int button, int key_flags) {
- mouse_over_warning_icon_ = true;
QueueDraw();
});
warning_->mouse_leave.connect([this] (int x, int y, int button, int key_flags) {
- mouse_over_warning_icon_ = false;
QueueDraw();
});
}
+void TextInput::UpdateScale(double scale)
+{
+ layout_->SetLeftAndRightPadding(LEFT_INTERNAL_PADDING.CP(scale), TEXT_INPUT_RIGHT_BORDER.CP(scale));
+ layout_->SetSpaceBetweenChildren(SPACE_BETWEEN_ENTRY_AND_HIGHLIGHT.CP(scale));
+
+ pango_entry_->SetFontSize(PANGO_ENTRY_FONT_SIZE.CP(scale));
+
+ int entry_min = pango_entry_->GetMinimumHeight();
+ pango_entry_->SetMaximumHeight(entry_min);
+ layered_layout_->SetMinimumHeight(entry_min);
+ layered_layout_->SetMaximumHeight(entry_min);
+
+ hint_layout_->SetLeftAndRightPadding(HINT_PADDING.CP(scale), HINT_PADDING.CP(scale));
+ hint_->SetScale(scale);
+ hint_->SetMaximumHeight(entry_min);
+
+ spinner_->scale = scale;
+ activator_->SetTexture(LoadActivatorIcon(DEFAULT_ICON_SIZE.CP(scale)));
+ warning_->SetTexture(LoadWarningIcon(DEFAULT_ICON_SIZE.CP(scale)));
+ warning_tooltip_.Release();
+
+ QueueRelayout();
+ QueueDraw();
+}
+
void TextInput::CheckIfCapsLockOn()
{
Display *dpy = nux::GetGraphicsDisplay()->GetX11Display();
@@ -217,11 +230,7 @@ void TextInput::CheckIfCapsLockOn()
void TextInput::SetSpinnerVisible(bool visible)
{
spinner_->SetVisible(visible);
-
- if (visible)
- activator_->SetVisible(false);
- else
- activator_->SetVisible(show_activator());
+ activator_->SetVisible(visible || show_activator());
}
void TextInput::SetSpinnerState(SpinnerState spinner_state)
@@ -299,9 +308,10 @@ void TextInput::LoadWarningTooltip()
extents.width += TOOLTIP_OFFSET;
extents.height += TOOLTIP_OFFSET;
- nux::CairoGraphics cg(CAIRO_FORMAT_ARGB32, extents.width, extents.height);
+ nux::CairoGraphics cg(CAIRO_FORMAT_ARGB32, RawPixel(extents.width).CP(scale), RawPixel(extents.height).CP(scale));
cairo_t* cr = cg.GetInternalContext();
+ cairo_surface_set_device_scale(cairo_get_target(cr), scale, scale);
gtk_render_background(style_context, cr, 0, 0, extents.width, extents.height);
gtk_render_frame(style_context, cr, 0, 0, extents.width, extents.height);
gtk_render_layout(style_context, cr, TOOLTIP_OFFSET/2, TOOLTIP_OFFSET/2, layout);
@@ -309,18 +319,18 @@ void TextInput::LoadWarningTooltip()
warning_tooltip_ = texture_ptr_from_cairo_graphics(cg);
}
-void TextInput::OnFontChanged(GtkSettings* settings, GParamSpec* pspec)
+void TextInput::OnFontChanged()
{
glib::String font_name;
PangoFontDescription* desc;
- g_object_get(settings, "gtk-font-name", &font_name, NULL);
+ g_object_get(gtk_settings_get_default(), "gtk-font-name", &font_name, NULL);
desc = pango_font_description_from_string(font_name.Value());
if (desc)
{
pango_entry_->SetFontFamily(pango_font_description_get_family(desc));
- pango_entry_->SetFontSize(PANGO_ENTRY_FONT_SIZE);
+ pango_entry_->SetFontSize(PANGO_ENTRY_FONT_SIZE.CP(scale));
pango_entry_->SetFontOptions(gdk_screen_get_font_options(gdk_screen_get_default()));
if (hint_font_name() == HINT_LABEL_DEFAULT_FONT_NAME)
@@ -328,8 +338,9 @@ void TextInput::OnFontChanged(GtkSettings* settings, GParamSpec* pspec)
std::ostringstream font_desc;
font_desc << pango_font_description_get_family(desc) << " " << hint_font_size();
hint_->SetFont(font_desc.str().c_str());
- pango_font_description_free(desc);
}
+
+ pango_font_description_free(desc);
}
}
@@ -377,8 +388,8 @@ void TextInput::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
layout_->ProcessDraw(GfxContext, force_draw);
- if (caps_lock_on && mouse_over_warning_icon_)
- PaintWarningTooltip(GfxContext);
+ if (caps_lock_on && warning_->IsMouseInside())
+ PaintWarningTooltip(GfxContext);
if (!IsFullRedraw())
{
@@ -394,29 +405,25 @@ void TextInput::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
void TextInput::PaintWarningTooltip(nux::GraphicsEngine& graphics_engine)
{
- nux::Geometry warning_geo = warning_->GetGeometry();
+ nux::Geometry const& warning_geo = warning_->GetGeometry();
+
+ if (!warning_tooltip_.IsValid())
+ LoadWarningTooltip();
- nux::Geometry tooltip_geo = {warning_geo.x - (warning_tooltip_->GetWidth() + TOOLTIP_OFFSET / 2),
- warning_geo.y - TOOLTIP_Y_OFFSET,
+ nux::Geometry tooltip_geo = {warning_geo.x - (warning_tooltip_->GetWidth() + TOOLTIP_OFFSET.CP(scale) / 2),
+ warning_geo.y - TOOLTIP_Y_OFFSET.CP(scale),
warning_tooltip_->GetWidth(),
warning_tooltip_->GetHeight()};
- nux::GetPainter().PushDrawLayer(graphics_engine, tooltip_geo, CreateWarningLayer(warning_tooltip_.GetPointer()));
+ auto const& warning_layer = CreateWarningLayer(warning_tooltip_.GetPointer());
+ nux::GetPainter().PushDrawLayer(graphics_engine, tooltip_geo, warning_layer.get());
}
void TextInput::UpdateBackground(bool force)
{
- int RADIUS = 5;
nux::Geometry geo(GetGeometry());
- LOG_DEBUG(logger) << "height: "
- << geo.height << " - "
- << layout_->GetGeometry().height << " - "
- << pango_entry_->GetGeometry().height;
-
- if (geo.width == last_width_
- && geo.height == last_height_
- && force == false)
+ if (geo.width == last_width_ && geo.height == last_height_ && !force)
return;
last_width_ = geo.width;
@@ -424,12 +431,13 @@ void TextInput::UpdateBackground(bool force)
nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, last_width_, last_height_);
cairo_t* cr = cairo_graphics.GetInternalContext();
+ cairo_surface_set_device_scale(cairo_get_target(cr), scale, scale);
cairo_graphics.DrawRoundedRectangle(cr,
1.0f,
0.5, 0.5,
- RADIUS,
- last_width_ - 1, last_height_ - 1,
+ BORDER_RADIUS,
+ (last_width_/scale) - 1, (last_height_/scale) - 1,
false);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
@@ -439,7 +447,7 @@ void TextInput::UpdateBackground(bool force)
cairo_set_source_rgba(cr, 1.0f, 1.0f, 1.0f, 0.7f);
cairo_stroke(cr);
- nux::BaseTexture* texture2D = texture_from_cairo_graphics(cairo_graphics);
+ auto texture2D = texture_ptr_from_cairo_graphics(cairo_graphics);
nux::TexCoordXForm texxform;
texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
@@ -455,8 +463,6 @@ void TextInput::UpdateBackground(bool force)
nux::color::White,
true,
rop));
-
- texture2D->UnReference();
}
void TextInput::OnKeyUp(unsigned keysym,
diff --git a/unity-shared/TextInput.h b/unity-shared/TextInput.h
index e7deb0401..aa8784450 100644
--- a/unity-shared/TextInput.h
+++ b/unity-shared/TextInput.h
@@ -64,6 +64,7 @@ public:
nux::ROProperty<bool> im_preedit;
nux::Property<bool> show_activator;
nux::Property<bool> show_caps_lock;
+ nux::Property<double> scale;
private:
void OnFontChanged();
@@ -71,6 +72,7 @@ private:
void Draw(nux::GraphicsEngine& GfxContext, bool force_draw);
void DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw);
void UpdateBackground(bool force);
+ void UpdateScale(double);
std::string GetName() const;
@@ -109,13 +111,13 @@ private:
std::unique_ptr<nux::AbstractPaintLayer> bg_layer_;
std::unique_ptr<nux::AbstractPaintLayer> highlight_layer_;
nux::HLayout* layout_;
+ nux::HLayout* hint_layout_;
nux::LayeredLayout* layered_layout_;
SearchBarSpinner* spinner_;
nux::Property<bool> caps_lock_on;
int last_width_;
int last_height_;
- bool mouse_over_warning_icon_;
IconTexture* warning_;
IconTexture* activator_;