HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_cursor.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility/utility.hpp"
8#include "../unicode/unicode.hpp"
9#include "../macros.hpp"
10#include <tuple>
11#include <cstdlib>
12#include <algorithm>
13
14hi_export_module(hikogui.text.text_cursor);
15
16
17hi_export namespace hi::inline v1 {
18
27public:
28 constexpr text_cursor() noexcept = default;
29 constexpr text_cursor(text_cursor const&) noexcept = default;
30 constexpr text_cursor(text_cursor&&) noexcept = default;
31 constexpr text_cursor& operator=(text_cursor const&) noexcept = default;
32 constexpr text_cursor& operator=(text_cursor&&) noexcept = default;
33 [[nodiscard]] constexpr friend bool operator==(text_cursor const&, text_cursor const&) = default;
34 [[nodiscard]] constexpr friend auto operator<=>(text_cursor const&, text_cursor const&) = default;
35
49 constexpr text_cursor& resize(size_t size) & noexcept
50 {
51 inplace_min(_value, max_value(size));
52 return *this;
53 }
54
55 constexpr text_cursor resize(size_t size) && noexcept
56 {
57 inplace_min(_value, max_value(size));
58 return *this;
59 }
60
61
67 constexpr text_cursor(size_t index, bool after) noexcept
68 {
69 _value = (index << 1) | static_cast<size_t>(after);
70 }
71
78 [[nodiscard]] constexpr text_cursor neighbor(size_t size) const noexcept
79 {
80 auto r = before() ? text_cursor{index() - 1, true} : text_cursor{index() + 1, false};
81 return r.resize(size);
82 }
83
84 [[nodiscard]] constexpr text_cursor after_neighbor(size_t size) const noexcept
85 {
86 return before() ? neighbor(size) : *this;
87 }
88
89 [[nodiscard]] constexpr text_cursor before_neighbor(size_t size) const noexcept
90 {
91 return after() ? neighbor(size) : *this;
92 }
93
94 [[nodiscard]] constexpr bool start_of_text() const noexcept
95 {
96 return _value == 0;
97 }
98
99 [[nodiscard]] constexpr bool end_of_text(size_t size) const noexcept
100 {
101 return _value >= max_value(size);
102 }
103
104 [[nodiscard]] constexpr size_t index() const noexcept
105 {
106 return _value >> 1;
107 }
108
109 [[nodiscard]] constexpr bool after() const noexcept
110 {
111 return to_bool(_value & 1);
112 }
113
114 [[nodiscard]] constexpr bool before() const noexcept
115 {
116 return not after();
117 }
118
119private:
125 [[nodiscard]] constexpr static size_t max_value(size_t size) noexcept
126 {
127 if (size) {
128 size <<= 1;
129 --size;
130 }
131 return size;
132 }
133
144 size_t _value = 0;
145};
146
147} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A cursor-position in text.
Definition text_cursor.hpp:26
constexpr text_cursor & resize(size_t size) &noexcept
Set the text size.
Definition text_cursor.hpp:49
constexpr text_cursor(size_t index, bool after) noexcept
Create a new text cursor.
Definition text_cursor.hpp:67
constexpr text_cursor neighbor(size_t size) const noexcept
Return the neighbor cursor.
Definition text_cursor.hpp:78