YSTest: BookmarkUI.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

BookmarkUI.cpp
浏览该文件的文档.
1 /*
2 © 2013-2015, 2018-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 "BookmarkUI.h" // for to_std_string, stoul;
29 #include "ShlReader.h"
30
31 namespace YReader
32 {
33
34 namespace
35 {
36
38 String
39 ConvertToUIString(Bookmark::PositionType pos, ShlTextReader& shl)
40 {
41 auto line(shl.GetSlice(pos, 48U));
42
43 if(line.size() > 1)
44 {
45 const bool b(line[0] == u'\n');
46 const auto i(line.find(u"\r\n", b));
47
48 if(i != string::npos)
49 line.erase(i);
50 if(b)
51 // XXX: Use cbegin after libstdc++ bug resolved.
52 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60278.
53 line.erase(line.begin());
54 }
55 // TODO: Use %to_u16string or %to_chars?
56 return String(to_string(pos)) + u" " + std::move(line);
57 }
59 vector<String>
60 ConvertToUIString(const BookmarkList& lst, ShlTextReader& shl)
61 {
62 vector<String> vec;
63
64 vec.reserve(lst.size());
65 for(const auto pos : lst)
66 vec.push_back(ConvertToUIString(pos, shl));
67 return vec;
68 }
69
70 BookmarkList
71 ConvertToBookmarkList(const vector<String>& lst)
72 {
73 BookmarkList bookmarks;
74
75 bookmarks.reserve(lst.size());
76 for(const auto& str : lst)
77 bookmarks.push_back(stoul(to_std_string(str.GetMBCS())));
78 return bookmarks;
79 }
80
81 } // unnamed namespace;
82
83 BookmarkPanel::BookmarkPanel(const BookmarkList& bm, ShlTextReader& shl)
84 : DialogPanel(Size(MainScreenWidth, MainScreenHeight)),
85 // XXX: Conversion to 'SPos' might be implementation-defined.
86 lbPosition({8, 32, 240, 128}),
87 btnAdd(Rect(SPos(GetWidth()) - 80, 4, 16, 16), 210),
88 btnRemove(Rect(SPos(GetWidth()) - 60, 4, 16, 16), 210), shell(shl),
89 bookmarks(bm)
90 {
91 const auto stop_routing_after_direct([](KeyEventArgs&& e) ynothrow{
92 if(e.Strategy == RoutedEventArgs::Bubble)
93 e.Handled = true;
94 });
95
96 AddWidgets(*this, lbPosition, btnAdd, btnRemove),
97 yunseq(
98 btnAdd.Text = u"+",
99 btnRemove.Text = u"-",
100 FetchEvent<KeyDown>(lbPosition) += stop_routing_after_direct,
101 FetchEvent<KeyHeld>(lbPosition) += stop_routing_after_direct,
102 FetchEvent<Click>(btnOK) += [this]{
103 bookmarks = ConvertToBookmarkList(lbPosition.GetList());
104 },
105 FetchEvent<Click>(btnAdd) += [this]{
106 auto& lst(lbPosition.GetListRef());
107 auto idx(GetSelected());
108
109 if(idx < 0)
110 // XXX: Conversion to 'ptrdiff_t' might be implementation-defined.
111 idx = ptrdiff_t(lst.size());
112 lst.insert(lst.begin() + idx,
113 String(ConvertToUIString(shell.get().GetReaderPosition(), shell)));
114 lbPosition.AdjustViewForContent();
115 lbPosition.UpdateView();
116 },
117 FetchEvent<Click>(btnRemove) += [this]{
118 auto& lst(lbPosition.GetListRef());
119 const auto idx(GetSelected());
120
121 if(idx >= 0)
122 {
123 lst.erase(lst.cbegin() + idx);
124 lbPosition.AdjustViewForContent();
125 lbPosition.UpdateView();
126 }
127 }
128 );
129 }
130 ImplDeDtor(BookmarkPanel)
131
132 BookmarkList::difference_type
133 BookmarkPanel::GetSelected() const
134 {
135 return lbPosition.IsSelected()
136 ? BookmarkList::difference_type(lbPosition.GetSelectedIndex()) : -1;
137 }
138
139 void
140 BookmarkPanel::LoadBookmarks()
141 {
142 lbPosition.GetListRef() = ConvertToUIString(bookmarks, shell);
143 }
144
145 } // namespace YReader;
146
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
YReader::ShlTextReader
文本阅读器 Shell 。
Definition: ShlReader.h:227
YSLib::Drawing::Size
屏幕区域大小。
Definition: YGDIBase.h:536
YReader
Definition: About.h:34
ImplDeDtor
#define ImplDeDtor(_t)
Definition: YBaseMacro.h:183
YReader::BookmarkPanel
书签管理面板。
Definition: BookmarkUI.h:45
ShlReader.h
Shell 阅读器框架。
platform::strings::to_std_string
_tString to_std_string(basic_string_view< _tChar > sv)
Definition: Container.h:256
YSLib::UI::RoutedEventArgs::Bubble
@ Bubble
气泡事件：向上遍历视图树时触发。
Definition: YWidgetEvent.h:109
YSLib::UI::AddWidgets
void AddWidgets(_tCon &con, _tWidgets &... wgts)
向部件容器添加子部件。
Definition: YUIContainer.h:446
YReader::BookmarkPanel::BookmarkPanel
BookmarkPanel(const BookmarkList &, ShlTextReader &)
Definition: BookmarkUI.cpp:83
BookmarkUI.h
书签界面。
YSLib::i
return i
Definition: YCoreUtilities.h:177
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
YSLib::UI::KeyEventArgs
按键输入事件参数类。
Definition: YWidgetEvent.h:193
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YReader::Bookmark::PositionType
size_t PositionType
文件位置类型。
Definition: ReadingList.h:49
YSLib::UI::DialogPanel
对话面板。
Definition: UIContainerEx.h:79
NPL::SXML::ParseOption::String
@ String
YSLib::Text::str
YB_ATTR_nodiscard YB_PURE const char const char * str
Definition: TextFile.h:83
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199