diff options
| author | Andrea Azzarone <azzaronea@gmail.com> | 2012-05-30 16:33:07 +0200 |
|---|---|---|
| committer | Andrea Azzarone <azzaronea@gmail.com> | 2012-05-30 16:33:07 +0200 |
| commit | d6b363260aaa5291d9b710eccb6283337a89cd40 (patch) | |
| tree | 2a97caa7d410af84a992ddc6db853e4239e0d49b | |
| parent | 6d54ac633a4b0951f27543bfc2006acff709f967 (diff) | |
Update the design of the hud button text. Use a full opacity for the highlighted text.
(bzr r2370.1.4)
| -rw-r--r-- | hud/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | hud/HudButton.cpp | 55 | ||||
| -rw-r--r-- | hud/HudButton.h | 11 | ||||
| -rw-r--r-- | hud/HudPrivate.cpp | 78 | ||||
| -rw-r--r-- | hud/HudPrivate.h | 42 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | tests/test_hud_button.cpp | 73 | ||||
| -rw-r--r-- | tests/test_hud_private.cpp | 77 | ||||
| -rw-r--r-- | unity-shared/StaticCairoText.cpp | 6 | ||||
| -rw-r--r-- | unity-shared/StaticCairoText.h | 1 |
10 files changed, 313 insertions, 38 deletions
diff --git a/hud/CMakeLists.txt b/hud/CMakeLists.txt index 0911793b9..0f4957107 100644 --- a/hud/CMakeLists.txt +++ b/hud/CMakeLists.txt @@ -32,6 +32,7 @@ set (HUD_SOURCES HudController.cpp HudIcon.cpp HudIconTextureSource.cpp + HudPrivate.cpp HudView.cpp ) diff --git a/hud/HudButton.cpp b/hud/HudButton.cpp index a8dc21fc5..f5e0d6d48 100644 --- a/hud/HudButton.cpp +++ b/hud/HudButton.cpp @@ -26,6 +26,7 @@ #include <gtk/gtk.h> #include <Nux/Nux.h> +#include <Nux/HLayout.h> #include <NuxCore/Logger.h> #include <NuxImage/CairoGraphics.h> #include <NuxGraphics/NuxGraphics.h> @@ -33,8 +34,10 @@ #include <UnityCore/Variant.h> #include "unity-shared/DashStyle.h" +#include "unity-shared/StaticCairoText.h" #include "HudButton.h" +#include "HudPrivate.h" namespace { @@ -46,42 +49,15 @@ namespace unity namespace hud { -HudButton::HudButton(nux::TextureArea *image, NUX_FILE_LINE_DECL) - : nux::Button(image, NUX_FILE_LINE_PARAM) - , is_rounded(false) - , is_focused_(false) -{ - Init(); -} - -HudButton::HudButton(std::string const& label_, NUX_FILE_LINE_DECL) - : nux::Button(NUX_FILE_LINE_PARAM) - , label(label_) - , is_rounded(false) - , is_focused_(false) -{ - Init(); -} - -HudButton::HudButton(std::string const& label_, nux::TextureArea *image, NUX_FILE_LINE_DECL) - : nux::Button(image, NUX_FILE_LINE_PARAM) - , label(label_) - , is_rounded(false) - , is_focused_(false) -{ - Init(); -} - HudButton::HudButton(NUX_FILE_LINE_DECL) : nux::Button(NUX_FILE_LINE_PARAM) , is_rounded(false) , is_focused_(false) { - Init(); -} + hlayout_ = new nux::HLayout(NUX_TRACKER_LOCATION); + hlayout_->SetLeftAndRightPadding(46, -1); + SetLayout(hlayout_); -void HudButton::Init() -{ InitTheme(); key_nav_focus_change.connect([&](nux::Area*, bool, nux::KeyNavDirection) @@ -119,7 +95,7 @@ void HudButton::InitTheme() void HudButton::RedrawTheme(nux::Geometry const& geom, cairo_t* cr, nux::ButtonVisualState faked_state) { - dash::Style::Instance().SquareButton(cr, faked_state, label, + dash::Style::Instance().SquareButton(cr, faked_state, "", is_rounded, 17, dash::Alignment::LEFT, true); } @@ -195,12 +171,25 @@ void HudButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw) void HudButton::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw) { + if (IsFullRedraw() && hlayout_) + hlayout_->ProcessDraw(GfxContext, force_draw); } void HudButton::SetQuery(Query::Ptr query) { query_ = query; label = query->formatted_text; + + auto items(impl::RefactorText(label)); + + hlayout_->Clear(); + for (auto item : items) + { + nux::StaticCairoText* text = new nux::StaticCairoText(item.first.c_str()); + text->SetTextColor(nux::Color(1.0f, 1.0f, 1.0f, item.second ? 1.0f : 0.5f)); + text->SetFont("Ubuntu 13"); // 17 px = 13 + hlayout_->AddView(text, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL); + } } Query::Ptr HudButton::GetQuery() @@ -220,5 +209,5 @@ void HudButton::AddProperties(GVariantBuilder* builder) .add("label", label()); } -} -} +} // namespace hud +} // namespace unity diff --git a/hud/HudButton.h b/hud/HudButton.h index d8b559cf5..555f5ab38 100644 --- a/hud/HudButton.h +++ b/hud/HudButton.h @@ -29,6 +29,8 @@ #include <UnityCore/Hud.h> #include "unity-shared/Introspectable.h" +namespace nux { class HLayout; } + namespace unity { namespace hud @@ -42,10 +44,7 @@ public: typedef nux::ObjectPtr<HudButton> Ptr; HudButton(NUX_FILE_LINE_PROTO); - HudButton(nux::TextureArea *image, NUX_FILE_LINE_PROTO); - HudButton(std::string const& label, NUX_FILE_LINE_PROTO); - HudButton(std::string const& label, nux::TextureArea* image, NUX_FILE_LINE_PROTO); - + void SetQuery(Query::Ptr query); std::shared_ptr<Query> GetQuery(); @@ -62,7 +61,6 @@ protected: std::string GetName() const; void AddProperties(GVariantBuilder* builder); - void Init(); void InitTheme(); void RedrawTheme(nux::Geometry const& geom, cairo_t* cr, nux::ButtonVisualState faked_state); @@ -70,9 +68,12 @@ private: Query::Ptr query_; nux::Geometry cached_geometry_; bool is_focused_; + NuxCairoPtr prelight_; NuxCairoPtr active_; NuxCairoPtr normal_; + + nux::HLayout* hlayout_; }; } // namespace hud diff --git a/hud/HudPrivate.cpp b/hud/HudPrivate.cpp new file mode 100644 index 000000000..8d491acc5 --- /dev/null +++ b/hud/HudPrivate.cpp @@ -0,0 +1,78 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the applicable version of the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of both the GNU Lesser General Public + * License version 3 along with this program. If not, see + * <http://www.gnu.org/licenses/> + * + * Authored by: Andrea Azzarone <azzaronea@gmail.com> + * Tim Penhey <tim.penhey@canonical.com> + * + */ + +#include "HudPrivate.h" + +namespace unity +{ +namespace hud +{ +namespace impl +{ + +std::vector<std::pair<std::string, bool>> RefactorText(std::string const& text) +{ + std::vector<std::pair<std::string, bool>> ret; + + static const std::string bold_start("<b>"); + static const std::string bold_end("</b>"); + + std::string::size_type last = 0; + std::string::size_type len = text.length(); + std::string::size_type pos = text.find(bold_start); + + while (pos != std::string::npos) + { + if (pos != last) + { + ret.push_back(std::pair<std::string, bool>(text.substr(last, pos - last), false)); + } + + // Look for the end + pos += 3; // // to skip the "<b>" + std::string::size_type end_pos = text.find(bold_end, pos); + // We hope we find it, if we don't, just output everything... + if (end_pos != std::string::npos) + { + ret.push_back(std::pair<std::string, bool>(text.substr(pos, end_pos - pos), true)); + last = end_pos + 4; // the length of "</b>" + pos = text.find(bold_start, last); + } + else + { + ret.push_back(std::pair<std::string, bool>(text.substr(pos), true)); + pos = std::string::npos; + last = len; + } + } + + if (last < len) + { + ret.push_back(std::pair<std::string, bool>(text.substr(last), false)); + } + + return ret; +} + +} // namespace unity +} // namespace hud +} // namespace impl diff --git a/hud/HudPrivate.h b/hud/HudPrivate.h new file mode 100644 index 000000000..419b3ca49 --- /dev/null +++ b/hud/HudPrivate.h @@ -0,0 +1,42 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the applicable version of the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of both the GNU Lesser General Public + * License version 3 along with this program. If not, see + * <http://www.gnu.org/licenses/> + * + * Authored by: Andrea Azzarone <azzaronea@gmail.com> + * + */ + +#ifndef UNITYSHELL_HUD_PRIVATE_H +#define UNITYSHELL_HUD_PRIVATE_H + +#include <string> +#include <utility> +#include <vector> + +namespace unity +{ +namespace hud +{ +namespace impl +{ + +std::vector<std::pair<std::string, bool>> RefactorText(std::string const& text); + +} // namespace impl +} // namespace hud +} // namespace unity + +#endif // UNITYSHELL_HUD_PRIVATE_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 78beb9085..89fcaa1b3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -148,6 +148,7 @@ if (GTEST_SRC_DIR AND test_grabhandle.cpp test_unityshell_private.cpp test_showdesktop_handler.cpp + test_hud_private.cpp ${CMAKE_SOURCE_DIR}/launcher/AbstractLauncherIcon.cpp ${CMAKE_SOURCE_DIR}/unity-shared/Animator.cpp ${UNITY_SRC}/DebugDBusInterface.cpp @@ -171,6 +172,7 @@ if (GTEST_SRC_DIR AND ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-impl-factory.cpp ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-grab-handle-layout.cpp ${CMAKE_SOURCE_DIR}/plugins/unity-mt-grab-handles/src/unity-mt-texture.cpp + ${CMAKE_SOURCE_DIR}/hud/HudPrivate.cpp ) target_link_libraries(test-gtest-xless gtest ${GMOCK_LIB} ${GMOCK_MAIN_LIB} ${LIBS}) add_test(UnityGTestXless test-gtest-xless) @@ -196,6 +198,7 @@ if (GTEST_SRC_DIR AND # Tests that require X add_executable(test-gtest + test_hud_button.cpp test_dashview_impl.cpp test_texture_cache.cpp test_main.cpp @@ -211,6 +214,8 @@ if (GTEST_SRC_DIR AND ${CMAKE_SOURCE_DIR}/dash/ResultRenderer.cpp ${CMAKE_SOURCE_DIR}/dash/ResultView.cpp ${CMAKE_SOURCE_DIR}/dash/ResultViewGrid.cpp + ${CMAKE_SOURCE_DIR}/hud/HudButton.cpp + ${CMAKE_SOURCE_DIR}/hud/HudPrivate.cpp ${CMAKE_SOURCE_DIR}/launcher/AbstractLauncherIcon.cpp ${CMAKE_SOURCE_DIR}/launcher/CairoBaseWindow.cpp ${CMAKE_SOURCE_DIR}/launcher/DNDCollectionWindow.cpp @@ -241,11 +246,13 @@ if (GTEST_SRC_DIR AND ${CMAKE_SOURCE_DIR}/launcher/SwitcherView.cpp ${CMAKE_SOURCE_DIR}/launcher/Tooltip.cpp ${CMAKE_SOURCE_DIR}/unity-shared/BackgroundEffectHelper.cpp + ${CMAKE_SOURCE_DIR}/unity-shared/DashStyle.cpp ${CMAKE_SOURCE_DIR}/unity-shared/IconLoader.cpp ${CMAKE_SOURCE_DIR}/unity-shared/IconRenderer.cpp ${CMAKE_SOURCE_DIR}/unity-shared/IconTextureSource.cpp ${CMAKE_SOURCE_DIR}/unity-shared/Introspectable.cpp ${CMAKE_SOURCE_DIR}/unity-shared/IntrospectableWrappers.cpp + ${CMAKE_SOURCE_DIR}/unity-shared/JSONParser.cpp ${CMAKE_SOURCE_DIR}/unity-shared/PanelStyle.cpp ${CMAKE_SOURCE_DIR}/unity-shared/StaticCairoText.cpp ${CMAKE_SOURCE_DIR}/unity-shared/TextureCache.cpp diff --git a/tests/test_hud_button.cpp b/tests/test_hud_button.cpp new file mode 100644 index 000000000..218b0fa8e --- /dev/null +++ b/tests/test_hud_button.cpp @@ -0,0 +1,73 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the applicable version of the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of both the GNU Lesser General Public + * License version 3 along with this program. If not, see + * <http://www.gnu.org/licenses/> + * + * Authored by: Andrea Azzarone <azzaronea@gmail.com> + * + */ + +#include <gtest/gtest.h> + +#include <Nux/Nux.h> +#include <Nux/Layout.h> +#include <UnityCore/Hud.h> + +#include "hud/HudButton.h" +#include "unity-shared/DashStyle.h" +#include "unity-shared/StaticCairoText.h" +#include "unity-shared/UnitySettings.h" + +using namespace unity; + +namespace +{ + +TEST(TestHudButton, TestLabelOpacity) +{ + Settings unity_settings; + dash::Style dash_style; + nux::ObjectPtr<hud::HudButton> button(new hud::HudButton()); + nux::Layout* layout = button->GetLayout(); + + ASSERT_NE(layout, nullptr); + ASSERT_EQ(layout->GetChildren().size(), 0); + + hud::Query::Ptr query(new hud::Query("<b>Op</b> Fi<b>le</b>", "","", "", "", NULL)); + button->SetQuery(query); + + auto children(layout->GetChildren()); + ASSERT_EQ(children.size(), 3); + + auto it = children.begin(); + nux::StaticCairoText* label = dynamic_cast<nux::StaticCairoText*>(*it); + ASSERT_NE(label, nullptr); + EXPECT_EQ(label->GetText(), "Op"); + EXPECT_EQ(label->GetTextColor().alpha, 1.0f); + + it++; + label = dynamic_cast<nux::StaticCairoText*>(*it); + ASSERT_NE(label, nullptr); + EXPECT_EQ(label->GetText(), " Fi"); + EXPECT_EQ(label->GetTextColor().alpha, 0.5f); + + it++; + label = dynamic_cast<nux::StaticCairoText*>(*it); + ASSERT_NE(label, nullptr); + EXPECT_EQ(label->GetText(), "le"); + EXPECT_EQ(label->GetTextColor().alpha, 1.0f); +} + +} diff --git a/tests/test_hud_private.cpp b/tests/test_hud_private.cpp new file mode 100644 index 000000000..d8e657cd1 --- /dev/null +++ b/tests/test_hud_private.cpp @@ -0,0 +1,77 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License version 3, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the applicable version of the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of both the GNU Lesser General Public + * License version 3 along with this program. If not, see + * <http://www.gnu.org/licenses/> + * + * Authored by: Andrea Azzarone <azzaronea@gmail.com> + * + */ + +#include <gtest/gtest.h> + +#include "hud/HudPrivate.h" +using namespace unity::hud; + +namespace +{ + +TEST(TestHudPrivate, RefactorTextEmpty) +{ + std::vector<std::pair<std::string, bool>> temp; + + temp = impl::RefactorText(""); + ASSERT_EQ(temp.size(), 0); + + temp = impl::RefactorText("Test"); + ASSERT_EQ(temp.size(), 1); + EXPECT_EQ(temp[0].first, "Test"); + EXPECT_EQ(temp[0].second, false); // True means "Full opacity", false "Half opacity" + + temp = impl::RefactorText("<b>Test</b>"); + ASSERT_EQ(temp.size(), 1); + EXPECT_EQ(temp[0].first, "Test"); + EXPECT_EQ(temp[0].second, true); + + temp = impl::RefactorText("Hello > <b>Test</b> World"); + ASSERT_EQ(temp.size(), 3); + EXPECT_EQ(temp[0].first, "Hello > "); + EXPECT_EQ(temp[0].second, false); + EXPECT_EQ(temp[1].first, "Test"); + EXPECT_EQ(temp[1].second, true); + EXPECT_EQ(temp[2].first, " World"); + EXPECT_EQ(temp[2].second, false); + + temp = impl::RefactorText("Open <b>Fi</b>le <b>Wit</b>h"); + ASSERT_EQ(temp.size(), 5); + EXPECT_EQ(temp[0].first, "Open "); + EXPECT_EQ(temp[0].second, false); + EXPECT_EQ(temp[1].first, "Fi"); + EXPECT_EQ(temp[1].second, true); + EXPECT_EQ(temp[2].first, "le "); + EXPECT_EQ(temp[2].second, false); + EXPECT_EQ(temp[3].first, "Wit"); + EXPECT_EQ(temp[3].second, true); + EXPECT_EQ(temp[4].first, "h"); + EXPECT_EQ(temp[4].second, false); + + temp = impl::RefactorText("Open <b>File With"); + ASSERT_EQ(temp.size(), 2); + EXPECT_EQ(temp[0].first, "Open "); + EXPECT_EQ(temp[0].second, false); + EXPECT_EQ(temp[1].first, "File With"); + EXPECT_EQ(temp[1].second, true); +} + +} diff --git a/unity-shared/StaticCairoText.cpp b/unity-shared/StaticCairoText.cpp index 80dbddbc1..a88619729 100644 --- a/unity-shared/StaticCairoText.cpp +++ b/unity-shared/StaticCairoText.cpp @@ -245,6 +245,12 @@ StaticCairoText::GetText() const return _text; } +nux::Color StaticCairoText::GetTextColor() const +{ + return _textColor; +} + + void StaticCairoText::SetTextColor(Color const& textColor) { diff --git a/unity-shared/StaticCairoText.h b/unity-shared/StaticCairoText.h index d4213a683..9cd07fd85 100644 --- a/unity-shared/StaticCairoText.h +++ b/unity-shared/StaticCairoText.h @@ -86,6 +86,7 @@ public: void SetLines(int maximum_lines); std::string GetText() const; + nux::Color GetTextColor() const; int GetLineCount(); int GetBaseline() const; |
