Skip to content

Commit

Permalink
Base UITraitCollection impl
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 19, 2024
1 parent 85f2733 commit 2d6058c
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ add_library(UIKit
lib/UIResponder.cpp
lib/UITapGestureRecognizer.cpp
lib/UITouch.cpp
lib/UITraitCollection.cpp
lib/UITraitEnvironment.cpp
lib/UIView.cpp
lib/UIViewAnimationGroup.cpp
lib/UIViewAnimationOptions.cpp
Expand Down
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/CABlurLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CABlurLayer: public CALayer {
void update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress) override;

private:
NXFloat _blurValue = 20;
NXFloat _blurValue = 10;
};

}
3 changes: 3 additions & 0 deletions Submodules/UIKit/include/SkiaCtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "include/core/SkSurface.h"
#include <functional>
#include "include/core/SkFontMgr.h"
#include <UITraitCollection.h>
#include <Geometry.h>

namespace NXKit {
Expand All @@ -23,6 +24,8 @@ class SkiaCtx {
virtual float getScaleFactor() { return 1; }
virtual sk_sp<SkFontMgr> getFontMgr() { return fontMgr; }

virtual UIUserInterfaceStyle getThemeMode() { return UIUserInterfaceStyle::light; }

static std::shared_ptr<SkiaCtx> main() { return _main; }
static std::shared_ptr<SkiaCtx> _main;
protected:
Expand Down
54 changes: 46 additions & 8 deletions Submodules/UIKit/include/UIColor.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#pragma once

#include <UITraitCollection.h>
#include <Geometry.h>
#include <stdint.h>
#include <functional>
#include <optional>

namespace NXKit {

class UIColor {
public:
UIColor();
UIColor(std::function<UIColor(std::shared_ptr<UITraitCollection>)> dynamicProvider);
UIColor(int rawValue);
UIColor(unsigned char red, unsigned char green, unsigned char blue);
UIColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);

Expand All @@ -19,27 +24,60 @@ class UIColor {
bool operator==(const UIColor& rhs) const;

UIColor interpolationTo(UIColor endResult, NXFloat progress);
uint32_t raw() const { return color; }
uint32_t raw() const;

// Transparent
static UIColor clear;

// Fixed
static UIColor black;
static UIColor darkGray;
static UIColor gray;
static UIColor lightGray;
static UIColor white;
static UIColor red;
static UIColor green;
static UIColor blue;
static UIColor orange;
static UIColor yellow;
static UIColor green;
static UIColor cyan;
static UIColor white;
static UIColor black;
static UIColor gray;
static UIColor blue;
static UIColor purple;
static UIColor magenta;
static UIColor brown;

// Adaptable
static UIColor systemRed;
static UIColor systemOrange;
static UIColor systemYellow;
static UIColor systemGreen;
static UIColor systemMint;
static UIColor systemTeal;
static UIColor systemCyan;
static UIColor systemBlue;
static UIColor systemIndigo;
static UIColor systemPurple;
static UIColor systemPink;
static UIColor systemBrown;

// Label
static UIColor label;
static UIColor secondaryLabel;
static UIColor tertiaryLabel;
static UIColor quaternaryLabel;

// Separator
static UIColor separator;

// Standard content background
static UIColor systemBackground;
static UIColor secondarySystemBackground;
static UIColor tetriarySystemBackground;

static UIColor tint;

private:
friend class CALayer;
uint32_t color;
std::optional<std::function<UIColor(std::shared_ptr<UITraitCollection>)>> dynamicProvider;
uint32_t color = 0xFF600060;
};

}
33 changes: 33 additions & 0 deletions Submodules/UIKit/include/UITraitCollection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>
#include <Geometry.h>

namespace NXKit {

enum class UIUserInterfaceStyle {
unspecified,
light,
dark
};

class UITraitCollection {
public:
std::shared_ptr<UITraitCollection> copy();

UIUserInterfaceStyle userInterfaceStyle() const { return _userInterfaceStyle; }
NXFloat displayScale() const { return _displayScale; }

static std::shared_ptr<UITraitCollection> current() { return _current; }
static void setCurrent(std::shared_ptr<UITraitCollection> current) { _current = current; }

private:
friend bool applicationRunLoop();

static std::shared_ptr<UITraitCollection> _current;

UIUserInterfaceStyle _userInterfaceStyle = UIUserInterfaceStyle::unspecified;
NXFloat _displayScale = 1;
};

}
24 changes: 24 additions & 0 deletions Submodules/UIKit/include/UITraitEnvironment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <UITraitCollection.h>

namespace NXKit {

class UITraitEnvironment {
public:
std::shared_ptr<UITraitCollection> traitCollection() const { return _traitCollection; }

virtual void traitCollectionDidChange(std::shared_ptr<UITraitCollection> previousTraitCollection);

protected:
friend bool applicationRunLoop();
friend class UIWindow;
friend class UIViewController;
friend class UIView;

std::shared_ptr<UITraitCollection> _traitCollection;

virtual void invalidateTraitCollection() {}
};

}
5 changes: 4 additions & 1 deletion Submodules/UIKit/include/UIView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "CALayer.h"
#include <UITraitEnvironment.h>
#include <UIViewContentMode.h>
#include <UIResponder.h>
#include <UIEvent.h>
Expand All @@ -11,7 +12,7 @@ namespace NXKit {
class UIWindow;
class UIViewController;
class UIGestureRecognizer;
class UIView: public UIResponder, public CALayerDelegate, public enable_shared_from_this<UIView> {
class UIView: public UIResponder, public UITraitEnvironment, public CALayerDelegate, public enable_shared_from_this<UIView> {
public:
UIView(NXRect frame = NXRect(), std::shared_ptr<CALayer> layer = new_shared<CALayer>());

Expand Down Expand Up @@ -65,6 +66,8 @@ class UIView: public UIResponder, public CALayerDelegate, public enable_shared_f

std::shared_ptr<CALayer> layer() const { return _layer; };

void traitCollectionDidChange(std::shared_ptr<UITraitCollection> previousTraitCollection) override;

// Layout
void setNeedsDisplay() { _needsDisplay = true; }
void setNeedsLayout();// { setNeedsDisplay(); _needsLayout = true; }
Expand Down
5 changes: 4 additions & 1 deletion Submodules/UIKit/include/UIViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NXKit {

class UIViewController: public UIResponder, public enable_shared_from_this<UIViewController> {
class UIViewController: public UIResponder, public UITraitEnvironment, public enable_shared_from_this<UIViewController> {
public:
std::map<std::string, std::shared_ptr<UIView>> idStorage;

Expand Down Expand Up @@ -45,6 +45,9 @@ class UIViewController: public UIResponder, public enable_shared_from_this<UIVie
void present(std::shared_ptr<UIViewController> otherViewController, bool animated, std::function<void()> completion = [](){});
void dismiss(bool animated, std::function<void()> completion = [](){});


void traitCollectionDidChange(std::shared_ptr<UITraitCollection> previousTraitCollection) override;

// Focus
// virtual std::shared_ptr<UIFocusEnvironment> parentFocusEnvironment() override;

Expand Down
1 change: 1 addition & 0 deletions Submodules/UIKit/include/UIWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UIWindow: public UIView {
virtual void pressesEnded(std::set<std::shared_ptr<UIPress>> pressees, std::shared_ptr<UIPressesEvent> event) override;
virtual void pressesCancelled(std::set<std::shared_ptr<UIPress>> pressees, std::shared_ptr<UIPressesEvent> event) override;

void traitCollectionDidChange(std::shared_ptr<UITraitCollection> previousTraitCollection) override;
private:
std::shared_ptr<UIViewController> _rootViewController;
std::vector<std::shared_ptr<UIViewController>> _presentedViewControllers;
Expand Down
2 changes: 2 additions & 0 deletions Submodules/UIKit/include/platforms/ios/SkiaCtx_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class SkiaCtx_ios : public SkiaCtx_sdlBase {

sk_sp<GrDirectContext> directContext() override { return context; }

NXKit::UIUserInterfaceStyle getThemeMode() override;

private:
sk_sp<GrDirectContext> context;
sk_sp<SkSurface> surface;
Expand Down
3 changes: 2 additions & 1 deletion Submodules/UIKit/include/tools/Tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <functional>

#include "OptionSet.hpp"
#include "SharedBase.hpp"

namespace NXKit {

Expand All @@ -26,4 +27,4 @@ std::optional<T> any_optional_cast(std::optional<std::any> obj) {
}
}

}
}
9 changes: 8 additions & 1 deletion Submodules/UIKit/lib/CABlurLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <CABlurLayer.h>
#include <tools/Tools.hpp>

#include "include/core/SkRRect.h"
#include "include/effects/SkImageFilters.h"

using namespace NXKit;
Expand Down Expand Up @@ -29,11 +30,17 @@ void CABlurLayer::draw(SkCanvas* context) {
context->save();

SkRect rect = SkRect::MakeXYWH(0, 0, bounds().width(), bounds().height());
context->clipRect(rect, true);
float radii = cornerRadius();
SkVector corners[] = {{radii, radii}, {radii, radii}, {radii, radii}, {radii, radii}};
SkRRect rrect;
rrect.setRectRadii(rect, corners);
context->clipRRect(rrect, true);
// // Make a separate layer using the blur filter, clipped to the middle rectangle's bounds
SkCanvas::SaveLayerRec slr(&rect, &blurPaint, SkCanvas::kInitWithPrevious_SaveLayerFlag);
context->saveLayer(slr);
context->restore();

context->drawColor(0x40FFFFFF);
context->restore();
}

Expand Down
4 changes: 2 additions & 2 deletions Submodules/UIKit/lib/CALayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <UIView.h>
#include <CATransaction.h>
#include <tools/Tools.hpp>
#include "include/core/SkRRect.h"

#include "include/core/SkRRect.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkImageFilters.h"

Expand Down Expand Up @@ -203,7 +203,7 @@ void CALayer::skiaRender(SkCanvas* canvas) {

// Background color
if (_backgroundColor.has_value()) {
paint.setColor(_backgroundColor->color);
paint.setColor(_backgroundColor->raw());

SkRect rect = SkRect::MakeXYWH(0, 0, _bounds.width(), _bounds.height());
SkRRect rrect;
Expand Down
14 changes: 13 additions & 1 deletion Submodules/UIKit/lib/UIApplicationMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ bool applicationRunLoop() {
// Move to UIRenderer
auto keyWindow = UIApplication::shared->keyWindow.lock();

auto scale = SkiaCtx::_main->getScaleFactor();

if (keyWindow->_traitCollection == nullptr
|| keyWindow->_traitCollection->_userInterfaceStyle != SkiaCtx::main()->getThemeMode()
|| keyWindow->_traitCollection->_displayScale != scale)
{
auto oldCollection = keyWindow->_traitCollection;
keyWindow->_traitCollection = new_shared<UITraitCollection>();
keyWindow->_traitCollection->_displayScale = scale;
keyWindow->_traitCollection->_userInterfaceStyle = SkiaCtx::main()->getThemeMode();
keyWindow->traitCollectionDidChange(oldCollection);
}

UIView::animateIfNeeded(currentTime);
keyWindow->drawAndLayoutTreeIfNeeded();

Expand All @@ -28,7 +41,6 @@ bool applicationRunLoop() {
canvas->clear(SK_ColorTRANSPARENT);

canvas->save();
auto scale = SkiaCtx::_main->getScaleFactor();
canvas->scale(scale, scale);

UIView::animate(0.3, [keyWindow]() {
Expand Down
Loading

0 comments on commit 2d6058c

Please sign in to comment.