From d3e2a751353e6c5b0e4d7c0a2af1cdbc09d3ea95 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 14 Oct 2021 21:58:14 +0800 Subject: ... --- src/common/String.cpp | 33 ++++++++++++++++----------------- src/osx/Convert.cpp | 4 ++-- src/osx/graphics/quartz/Font.cpp | 3 +++ src/osx/graphics/quartz/TextLayout.cpp | 3 +++ src/osx/gui/UiApplication.mm | 26 ++++++++++++++++++++++++++ src/osx/gui/Window.mm | 2 ++ 6 files changed, 52 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/common/String.cpp b/src/common/String.cpp index 092ba406..ef5eb176 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -17,7 +17,7 @@ String String::FromUtf8(const char* str, Index size) { return FromUtf16(cru::ToUtf16(std::string_view(str, size))); } -std::uint16_t String::kEmptyBuffer[1] = {0}; +char16_t String::kEmptyBuffer[1] = {0}; template Index GetStrSize(const C* str) { @@ -28,10 +28,10 @@ Index GetStrSize(const C* str) { return i; } -String::String(const std::uint16_t* str) : String(str, GetStrSize(str)) {} +String::String(const_pointer str) : String(str, GetStrSize(str)) {} -String::String(const std::uint16_t* str, Index size) { - this->buffer_ = new std::uint16_t[size + 1]; +String::String(const_pointer str, Index size) { + this->buffer_ = new value_type[size + 1]; std::memcpy(this->buffer_, str, size * sizeof(char16_t)); this->buffer_[size] = 0; this->size_ = size; @@ -46,7 +46,7 @@ String::String(const wchar_t* str, Index size) String::String(const String& other) { if (other.size_ == 0) return; - this->buffer_ = new std::uint16_t[other.size_ + 1]; + this->buffer_ = new value_type[other.size_ + 1]; std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(value_type)); this->buffer_[other.size_] = 0; this->size_ = other.size_; @@ -73,7 +73,7 @@ String& String::operator=(const String& other) { this->size_ = 0; this->capacity_ = 0; } else { - this->buffer_ = new std::uint16_t[other.size_ + 1]; + this->buffer_ = new value_type[other.size_ + 1]; std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(value_type)); this->buffer_[other.size_] = 0; @@ -106,8 +106,7 @@ String::~String() { } } -String::String(from_buffer_tag, std::uint16_t* buffer, Index size, - Index capacity) +String::String(from_buffer_tag, pointer buffer, Index size, Index capacity) : buffer_(buffer), size_(size), capacity_(capacity) {} void String::resize(Index new_size) { @@ -120,7 +119,7 @@ void String::resize(Index new_size) { buffer_[size_] = 0; } else { reserve(new_size); - std::memset(buffer_ + size_, 0, sizeof(std::uint16_t) * (new_size - size_)); + std::memset(buffer_ + size_, 0, sizeof(value_type) * (new_size - size_)); buffer_[new_size] = 0; size_ = new_size; } @@ -146,9 +145,9 @@ void String::reserve(Index new_capacity) { Expects(new_capacity >= 0); if (new_capacity <= this->capacity_) return; if (new_capacity > 0) { - std::uint16_t* new_buffer = new std::uint16_t[new_capacity + 1]; + pointer new_buffer = new value_type[new_capacity + 1]; if (this->buffer_ != kEmptyBuffer) { - memcpy(new_buffer, this->buffer_, this->size_ * sizeof(std::uint16_t)); + memcpy(new_buffer, this->buffer_, this->size_ * sizeof(value_type)); delete[] this->buffer_; } new_buffer[this->size_] = 0; @@ -157,11 +156,11 @@ void String::reserve(Index new_capacity) { } } -String::iterator String::insert(const_iterator pos, const std::uint16_t* str, +String::iterator String::insert(const_iterator pos, const_iterator str, Index size) { Expects(pos >= cbegin() && pos <= cend()); - std::vector backup_buffer; + std::vector backup_buffer; if (str >= buffer_ && str < buffer_ + size_) { backup_buffer.resize(size); std::copy(str, str + size, backup_buffer.begin()); @@ -201,7 +200,7 @@ String::iterator String::erase(const_iterator start, const_iterator end) { auto s = const_cast(start); auto e = const_cast(end); - std::memmove(e, s, (cend() - end) * sizeof(std::uint16_t)); + std::memmove(e, s, (cend() - end) * sizeof(value_type)); this->size_ = new_size; this->buffer_[new_size] = 0; @@ -209,7 +208,7 @@ String::iterator String::erase(const_iterator start, const_iterator end) { } std::string String::ToUtf8() const { - return cru::ToUtf8(std::u16string_view(Char16CStr(), size())); + return cru::ToUtf8(std::u16string_view(data(), size())); } Index String::IndexFromCodeUnitToCodePoint(Index code_unit_index) const { @@ -250,7 +249,7 @@ String& String::operator+=(const String& other) { } namespace { -inline int Compare(std::uint16_t left, std::uint16_t right) { +inline int Compare(char16_t left, char16_t right) { if (left < right) return -1; if (left > right) return 1; return 0; @@ -286,7 +285,7 @@ namespace details { std::vector ParseToFormatTokenList(const String& str) { std::vector result; - auto push_char = [&result](std::uint16_t c) { + auto push_char = [&result](char16_t c) { if (result.empty() || result.back().type == FormatTokenType::PlaceHolder) { result.push_back(FormatToken{FormatTokenType::Text, String{}}); } diff --git a/src/osx/Convert.cpp b/src/osx/Convert.cpp index 51ebedf4..a237a46f 100644 --- a/src/osx/Convert.cpp +++ b/src/osx/Convert.cpp @@ -1,6 +1,6 @@ #include "cru/osx/Convert.hpp" -namespace cru::cru::platform::osx { +namespace cru::platform::osx { CFStringRef Convert(const String& string) { return CFStringCreateWithBytes( nullptr, reinterpret_cast(string.data()), @@ -12,7 +12,7 @@ String Convert(CFStringRef string) { kCFStringEncodingUTF16, 0); auto l = CFDataGetLength(d); - auto s = String(reinterpret_cast(CFDataGetBytePtr(d)), + auto s = String(reinterpret_cast(CFDataGetBytePtr(d)), CFDataGetLength(d) / 2); CFRelease(d); diff --git a/src/osx/graphics/quartz/Font.cpp b/src/osx/graphics/quartz/Font.cpp index 600f8309..596489e4 100644 --- a/src/osx/graphics/quartz/Font.cpp +++ b/src/osx/graphics/quartz/Font.cpp @@ -1,9 +1,12 @@ #include "cru/osx/graphics/quartz/Font.hpp" +#include "cru/osx/Convert.hpp" #include "cru/osx/graphics/quartz/Convert.hpp" #include "cru/osx/graphics/quartz/Resource.hpp" namespace cru::platform::graphics::osx::quartz { +using cru::platform::osx::Convert; + OsxCTFont::OsxCTFont(IGraphicsFactory* graphics_factory, const String& name, float size) : OsxQuartzResource(graphics_factory) { diff --git a/src/osx/graphics/quartz/TextLayout.cpp b/src/osx/graphics/quartz/TextLayout.cpp index 577eba58..6c6644e5 100644 --- a/src/osx/graphics/quartz/TextLayout.cpp +++ b/src/osx/graphics/quartz/TextLayout.cpp @@ -1,4 +1,5 @@ #include "cru/osx/graphics/quartz/TextLayout.hpp" +#include "cru/osx/Convert.hpp" #include "cru/osx/graphics/quartz/Convert.hpp" #include "cru/osx/graphics/quartz/Resource.hpp" #include "cru/platform/Check.hpp" @@ -7,6 +8,8 @@ #include namespace cru::platform::graphics::osx::quartz { +using cru::platform::osx::Convert; + OsxCTTextLayout::OsxCTTextLayout(IGraphicsFactory* graphics_factory, std::shared_ptr font, const String& str) diff --git a/src/osx/gui/UiApplication.mm b/src/osx/gui/UiApplication.mm index 2491390f..d2e6fe30 100644 --- a/src/osx/gui/UiApplication.mm +++ b/src/osx/gui/UiApplication.mm @@ -1,13 +1,18 @@ #include "cru/osx/gui/UiApplication.hpp" #include "cru/osx/graphics/quartz/Factory.hpp" +#include "cru/osx/gui/Window.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" #include #include #include +#include #include #include +#include @interface AppDelegate : NSObject - (id)init:(cru::platform::gui::osx::details::OsxUiApplicationPrivate*)p; @@ -43,6 +48,8 @@ class OsxUiApplicationPrivate { std::unordered_map> next_tick_; std::unordered_map timers_; + std::vector windows_; + std::unique_ptr quartz_graphics_factory_; }; @@ -122,6 +129,25 @@ void OsxUiApplication::CancelTimer(long long id) { p_->timers_.erase(i); } } + +std::vector OsxUiApplication::GetAllWindow() { + std::vector result; + std::transform(p_->windows_.cbegin(), p_->windows_.cend(), std::back_inserter(result), + [](OsxWindow* w) { return static_cast(w); }); + return result; +} + +INativeWindow* OsxUiApplication::CreateWindow(INativeWindow* parent, CreateWindowFlag flags) { + auto window = new OsxWindow(this, parent, !(flags & CreateWindowFlags::NoCaptionAndBorder)); + p_->windows_.push_back(window); + return window; +} + +void OsxUiApplication::UnregisterWindow(OsxWindow* window) { + p_->windows_.erase( + std::remove(p_->windows_.begin(), p_->windows_.end(), static_cast(window)), + p_->windows_.cend()); +} } @implementation AppDelegate diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm index ff5572f2..c2134c76 100644 --- a/src/osx/gui/Window.mm +++ b/src/osx/gui/Window.mm @@ -92,6 +92,8 @@ OsxWindow::~OsxWindow() { if (p_->window_) { [p_->window_ close]; } + + dynamic_cast(GetUiApplication())->UnregisterWindow(this); } void OsxWindow::Close() { -- cgit v1.2.3