YSTest: ShellHelper.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

ShellHelper.cpp
浏览该文件的文档.
1 /*
2 © 2010-2015, 2019 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "Helper/YModules.h"
29 #include YFM_Helper_ShellHelper
30 #include YFM_Helper_GUIApplication // for FetchGlobalInstance;
31 #include YFM_Helper_Initialization // for FetchDefaultFontCache;
32 #include YFM_YSLib_Service_FileSystem // for IO::*;
33 #include <cstdio> // for std::sprintf;
34
35 namespace YSLib
36 {
37
38 #ifndef NDEBUG
39 DebugTimer::DebugTimer(string_view str)
40 : event_info((Nonnull(str.data()), str)), base_tick()
41 {
42 YTraceDe(0xF0, "Start tick of [%s] :", event_info.c_str());
43 base_tick = Timers::HighResolutionClock::now();
44 }
45 DebugTimer::~DebugTimer()
46 {
47 const double t((Timers::HighResolutionClock::now() - base_tick).count()
48 / 1e6);
49
50 YTraceDe(0xF0, "Performed [%s] in: %f milliseconds.", event_info.c_str(),
51 t);
52 }
53 #endif
54
55
56 void
57 RemoveGlobalTasks()
58 {
59 auto& app(FetchGlobalInstance());
60
61 app.AccessQueue([&](MessageQueue& mq){
62 return mq.Remove(app.UIResponseLimit);
63 });
64 }
65
66
67 namespace
68 {
69
71 inline void
72 snftime(char* buf, size_t n, const std::tm& tm,
73 const char* format = DefaultTimeFormat)
74 {
75 // FIXME: correct behavior for time with BC date(i.e. tm_year < -1900);
76 std::snprintf(buf, n, format, tm.tm_year + 1900,
77 tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
78 }
79
80 } // unnamed namespace;
81
82
83 const char*
84 TranslateTime(const std::tm& tm, const char* format)
85 {
86 static char str[80];
87
88 // NOTE: 'std::strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &tm)'
89 // is correct but it makes the object file too large.
90 // TODO: Check the return value?
91 snftime(str, 80, tm, format);
92 return str;
93 }
94 const char*
95 TranslateTime(const std::time_t& t, const char* format)
96 {
97 auto p(std::localtime(&t));
98
99 if(YB_UNLIKELY(!p))
100 throw GeneralEvent("Get broken-down time object failed"
101 " @ TranslateTime#2;");
102 return TranslateTime(*p, format);
103 }
104
105
106 shared_ptr<UI::TextList::ListType>
107 FetchFontFamilyNames()
108 {
109 const auto& mFamilies(FetchDefaultFontCache().GetFamilyIndices());
110
111 return make_shared<UI::TextList::ListType>(mFamilies.cbegin()
112 | ystdex::get_key, mFamilies.cend() | ystdex::get_key);
113 }
114
115
116 FPSCounter::FPSCounter(std::chrono::nanoseconds s)
117 : last_tick(Timers::GetHighResolutionTicks()), now_tick(),
118 refresh_count(1), MinimalInterval(s)
119 {}
120
121 size_t
122 FPSCounter::Refresh()
123 {
124 using namespace std::chrono;
125 const nanoseconds tmp_tick(Timers::GetHighResolutionTicks());
126
127 if(YB_UNLIKELY(last_tick + MinimalInterval < tmp_tick))
128 {
129 last_tick = now_tick;
130
131 const auto r(nanoseconds::rep(1000000000000ULL * refresh_count)
132 / ((now_tick = tmp_tick) - last_tick).count());
133
134 refresh_count = 1;
135 return size_t(r);
136 }
137 else
138 ++refresh_count;
139 return 0;
140 }
141
142 namespace UI
143 {
144
145 void
146 SwitchVisible(IWidget& wgt)
147 {
148 SetVisibleOf(wgt, !IsVisible(wgt));
149 Invalidate(wgt);
150 }
151
152 void
153 SwitchVisibleToFront(IWidget& wgt)
154 {
155 SetVisibleOf(wgt, !IsVisible(wgt));
156 RequestToFront(wgt);
157 }
158
159
160 shared_ptr<TextList::ListType>
161 FetchVisualStyleNames(String default_name)
162 {
163 const auto& styles(FetchGUIConfiguration().Styles);
164 auto p_list(make_shared<TextList::ListType>(styles.cbegin()
165 | ystdex::get_key, styles.cend() | ystdex::get_key));
166
167 YAssert(bool(p_list) && !p_list->empty(),
168 "Invalid style mapping state found");
169 p_list->front() = std::move(default_name);
170 return make_shared<TextList::ListType>(p_list->cbegin(), p_list->cend());
171 }
172
173 } // namespace UI;
174
175 } // namespace YSLib;
176
platform::Nonnull
YB_PURE _type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:518
YSLib::DefaultTimeFormat
yconstexpr const char *const DefaultTimeFormat("%04u-%02u-%02u %02u:%02u:%02u")
默认时间格式字符串。
YSLib::UI::FetchVisualStyleNames
YF_API shared_ptr< TextList::ListType > FetchVisualStyleNames(String=u"<Default>")
取视觉样式名称。
Definition: ShellHelper.cpp:161
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YSLib::FPSCounter::FPSCounter
FPSCounter(std::chrono::nanoseconds={})
构造：使用指定计时间隔下界。
Definition: ShellHelper.cpp:116
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
YSLib::UI::RequestToFront
void RequestToFront(IWidget &wgt)
Definition: YWidget.cpp:195
YSLib::FPSCounter::now_tick
std::chrono::nanoseconds now_tick
Definition: ShellHelper.h:231
YSLib::DebugTimer::DebugTimer
DebugTimer(string_view="")
Definition: ShellHelper.cpp:39
YSLib::DebugTimer::~DebugTimer
~DebugTimer()
Definition: ShellHelper.cpp:45
YSLib::FetchFontFamilyNames
YF_API shared_ptr< UI::TextList::ListType > FetchFontFamilyNames()
取字型家族名称。
Definition: ShellHelper.cpp:107
YSLib::FetchDefaultFontCache
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
Definition: Initialization.cpp:822
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: YException.h:46
YModules.h
Helper 库模块配置文件。
platform::GetHighResolutionTicks
YF_API std::uint64_t GetHighResolutionTicks() ynothrow
取高精度 tick 数。
Definition: Timer.cpp:88
YSLib::FetchGlobalInstance
_tApp & FetchGlobalInstance()
Definition: GUIApplication.h:271
NPL::tm
tm
Definition: NPLA.h:1941
YSLib::UI::Invalidate
YF_API void Invalidate(IWidget &, const Rect &)
无效化：使相对部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.cpp:108
YSLib::FPSCounter::refresh_count
size_t refresh_count
刷新计数。
Definition: ShellHelper.h:236
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
ystdex::cpp2017::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:581
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:47
ystdex::get_key
const first_tag get_key
Definition: iterator.hpp:559
YSLib::Messaging::MessageQueue
消息队列。
Definition: YMessage.h:159
YSLib::UI::FetchGUIConfiguration
GUIConfiguration & FetchGUIConfiguration()
Definition: YGUI.cpp:517
YSLib::RemoveGlobalTasks
void RemoveGlobalTasks()
Definition: ShellHelper.cpp:57
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:407
YSLib::FPSCounter::Refresh
now_tick size_t Refresh()
刷新：更新计数器内部计数。
Definition: ShellHelper.cpp:122
YSLib::DebugTimer::base_tick
Timers::HighResolutionClock::time_point base_tick
Definition: ShellHelper.h:55
YSLib::UI::SwitchVisibleToFront
YF_API void SwitchVisibleToFront(IWidget &)
切换部件显示状态并请求提升至前端。
Definition: ShellHelper.cpp:153
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YSLib::TranslateTime
YF_API YB_ATTR_returns_nonnull const char * TranslateTime(const std::tm &, const char *=DefaultTimeFormat)
格式化时间字符串。
Definition: ShellHelper.cpp:84
YSLib::str
const char * str
Definition: YException.cpp:60
YSLib::Messaging::MessageQueue::Remove
void Remove(Priority)
移除不大于指定优先级的消息。
Definition: YMessage.cpp:81
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:113
YSLib::Text::buf
YB_ATTR_nodiscard YB_PURE const char * buf
Definition: TextFile.h:83
YSLib::UI::SwitchVisible
YF_API void SwitchVisible(IWidget &)
切换部件显示状态并无效化。
Definition: ShellHelper.cpp:146
YSLib::DebugTimer::event_info
string event_info
Definition: ShellHelper.h:54
YSLib::FPSCounter::last_tick
std::chrono::nanoseconds last_tick
内部计数。
Definition: ShellHelper.h:231
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:828
platform::p
return p p
Definition: YCLib/FileSystem.h:209
YSLib::size_t
YF_API const char size_t
Definition: YException.h:149
YSLib::UI::IsVisible
IsVisible
Definition: YWidget.h:101
YSLib::FPSCounter::MinimalInterval
std::chrono::nanoseconds MinimalInterval
计时间隔下界。
Definition: ShellHelper.h:243