Skip to content

Commit

Permalink
UIViewController impl
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 15, 2024
1 parent 96fefe0 commit b7557f6
Show file tree
Hide file tree
Showing 16 changed files with 751 additions and 114 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ add_libromfs(${PROJECT_NAME} ${PROJECT_RESOURCES})
add_executable(${PROJECT_NAME}
app/main.cpp
app/AppDelegate.cpp
app/Screens/TestViewController/TestViewController.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC
app
)

setup_project()
Expand Down
10 changes: 6 additions & 4 deletions Submodules/UIKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_library(UIKit
lib/UIView.cpp
lib/UIViewAnimationGroup.cpp
lib/UIViewAnimationOptions.cpp
lib/UIViewController.cpp
lib/UIWindow.cpp
lib/NXAffineTransform.cpp
lib/NXTransform3D.cpp
Expand Down Expand Up @@ -63,16 +64,17 @@ if (APPLE)
)
# IOS
elseif (PLATFORM_IOS)
list(APPEND platform_sources
lib/platforms/apple/ios/SkiaCtx_ios.mm
)

find_library(SDL2 NAMES SDL2-static SDL2main)

list(APPEND platform_libs
SDL2-static SDL2main
${EXTERN_PATH}/skia/out/ios-arm64-angle/libskia.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libwindow.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libicu.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libskshaper.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libskunicode_core.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libskunicode_icu.a
${EXTERN_PATH}/skia/out/ios-arm64-angle/libskparagraph.a
)

target_sources(UIKit PRIVATE
Expand Down
18 changes: 17 additions & 1 deletion Submodules/UIKit/include/CATextLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

namespace NXKit {

enum class NSTextAlignment {
left,
right,
center,
justified,
natural
};

class CATextLayer: public CALayer {
public:
CATextLayer();
Expand All @@ -24,20 +32,28 @@ class CATextLayer: public CALayer {
void setFontSize(NXFloat fontSize);
[[nodiscard]] NXFloat fontSize() const { return _fontSize; }

void setFontWeight(NXFloat fontWeight);
[[nodiscard]] NXFloat fontWeight() const { return _fontWeight; }

void setTextColor(UIColor textColor);
[[nodiscard]] UIColor textColor() const { return _textColor; }

void setTextAlignment(NSTextAlignment textAlignment);
[[nodiscard]] NSTextAlignment textAlignment() const { return _textAlignment; }

std::optional<AnimatableProperty> value(std::string forKeyPath) override;
protected:
void update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress) override;

private:
NXFloat _fontSize = 17;
NXFloat _fontWeight = SkFontStyle::kNormal_Weight;
NSTextAlignment _textAlignment = NSTextAlignment::left;
std::string _text = "Furthermore, العربية نص جميل. द क्विक ब्राउन फ़ॉक्स jumps over the lazy 🐕.";
UIColor _textColor = UIColor::black;

// Skia
sk_sp<SkTypeface> typeface;
// sk_sp<SkTypeface> typeface;
// skia::textlayout::ParagraphStyle paraStyle;
// sk_sp<skia::textlayout::FontCollection> fontCollection;
sk_sp<SkUnicode> unicode;
Expand Down
6 changes: 5 additions & 1 deletion Submodules/UIKit/include/UIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
// Created by Даниил Виноградов on 17.11.2024.
//

#pragma once
#pragma once

#include <UIViewController.h>
#include <UILabel.h>
#include <UIImageView.h>
18 changes: 15 additions & 3 deletions Submodules/UIKit/include/UILabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ class UILabel: public UIView {
public:
UILabel();

void setTextColor(UIColor textColor);
[[nodiscard]] UIColor textColor();
void setText(std::string text) { _textLayer()->setText(text); }
[[nodiscard]] std::string text() const { return _textLayer()->text(); }

void setTextColor(UIColor textColor) { _textLayer()->setTextColor(textColor); }
[[nodiscard]] UIColor textColor() const { return _textLayer()->textColor(); }

void setFontSize(NXFloat fontSize) { _textLayer()->setFontSize(fontSize); }
[[nodiscard]] NXFloat fontSize() const { return _textLayer()->fontSize(); }

void setTextAlignment(NSTextAlignment textAlignment) { _textLayer()->setTextAlignment(textAlignment); }
[[nodiscard]] NSTextAlignment textAlignment() const { return _textLayer()->textAlignment(); }

void setFontWeight(NXFloat fontWeight) { _textLayer()->setFontWeight(fontWeight); }
[[nodiscard]] NXFloat fontWeight() const { return _textLayer()->fontWeight(); }

private:
int _numberOfLines = 1;
std::string _text;

std::shared_ptr<CATextLayer> _textLayer();
std::shared_ptr<CATextLayer> _textLayer() const;
};

}
19 changes: 12 additions & 7 deletions Submodules/UIKit/include/UIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace NXKit {

class UIWindow;
class UIViewController;
class UIView: public CALayerDelegate, public enable_shared_from_this<UIView> {
public:
UIView(): UIView(NXRect()) {}
Expand Down Expand Up @@ -49,7 +51,7 @@ class UIView: public CALayerDelegate, public enable_shared_from_this<UIView> {
void insertSubviewBelow(std::shared_ptr<UIView> view, std::shared_ptr<UIView> belowSubview);
void removeFromSuperview();

// virtual std::shared_ptr<UIWindow> window();
virtual std::shared_ptr<UIWindow> window();

const std::vector<std::shared_ptr<UIView>>& subviews() const { return _subviews; }
std::weak_ptr<UIView> superview() const { return _superview; }
Expand All @@ -59,12 +61,12 @@ class UIView: public CALayerDelegate, public enable_shared_from_this<UIView> {
// Layout
void setNeedsDisplay() { _needsDisplay = true; }
void setNeedsLayout();// { setNeedsDisplay(); _needsLayout = true; }
//
// void layoutIfNeeded();
// virtual void layoutSubviews();
//
// virtual Size sizeThatFits(Size size);
// virtual void sizeToFit();

void layoutIfNeeded();
virtual void layoutSubviews();

virtual NXSize sizeThatFits(NXSize size);
virtual void sizeToFit();

void drawAndLayoutTreeIfNeeded();

Expand Down Expand Up @@ -97,11 +99,14 @@ class UIView: public CALayerDelegate, public enable_shared_from_this<UIView> {
virtual void draw() {}
virtual void display(std::shared_ptr<CALayer> layer) override;
private:
friend class UIViewController;

std::vector<std::shared_ptr<UIView>> _subviews;
std::weak_ptr<UIView> _superview;
std::shared_ptr<CALayer> _layer;
std::shared_ptr<UIView> _mask;
UIViewContentMode _contentMode;
std::weak_ptr<UIViewController> _parentController;
bool _isUserInteractionEnabled = true;

bool _needsLayout = true;
Expand Down
70 changes: 70 additions & 0 deletions Submodules/UIKit/include/UIViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <UIView.h>

namespace NXKit {

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

void setView(std::shared_ptr<UIView> view);
std::shared_ptr<UIView> view();
bool viewIsLoaded();
void loadViewIfNeeded();

// std::shared_ptr<UIResponder> next() override;

// Most of these methods are designed to be overriden in `UIViewController` subclasses
virtual void loadView();
virtual void viewDidLoad() {}
virtual void viewWillAppear(bool animated);
virtual void viewDidAppear(bool animated);
virtual void viewWillDisappear(bool animated);
virtual void viewDidDisappear(bool animated);

virtual void viewWillLayoutSubviews() {}
virtual void viewDidLayoutSubviews() {}

std::vector<std::shared_ptr<UIViewController>> children() { return _children; }
std::weak_ptr<UIViewController> parent() { return _parent; }

void addChild(const std::shared_ptr<UIViewController>& child);
virtual void willMoveToParent(const std::shared_ptr<UIViewController>& parent);
virtual void didMoveToParent(std::shared_ptr<UIViewController> parent);
void removeFromParent();

// UIEdgeInsets additionalSafeAreaInsets() { return _additionalSafeAreaInsets; }
// void setAdditionalSafeAreaInsets(UIEdgeInsets additionalSafeAreaInsets);

// UIEdgeInsets systemMinimumLayoutMargins() { return _systemMinimumLayoutMargins; }

bool viewRespectsSystemMinimumLayoutMargins() { return _viewRespectsSystemMinimumLayoutMargins; }
void setViewRespectsSystemMinimumLayoutMargins(bool viewRespectsSystemMinimumLayoutMargins);

void present(std::shared_ptr<UIViewController> otherViewController, bool animated, std::function<void()> completion = [](){});
void dismiss(bool animated, std::function<void()> completion = [](){});

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

protected:
virtual void makeViewAppear(bool animated, std::shared_ptr<UIViewController> presentingViewController, std::function<void()> completion = [](){});
virtual void makeViewDisappear(bool animated, std::function<void(bool)> completion);

private:
std::shared_ptr<UIView> _view;
std::weak_ptr<UIViewController> _parent;
std::vector<std::shared_ptr<UIViewController>> _children;
// UIEdgeInsets _additionalSafeAreaInsets;
// UIEdgeInsets _systemMinimumLayoutMargins = UIEdgeInsets(0, 16, 0, 16);
bool _viewRespectsSystemMinimumLayoutMargins = true;
float _animationTime = 0.5;

std::shared_ptr<UIViewController> _presentedViewController;
std::weak_ptr<UIViewController> _presentingViewController;

std::shared_ptr<UIViewController> rootVC();
};

}
33 changes: 32 additions & 1 deletion Submodules/UIKit/include/UIWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,41 @@

namespace NXKit {

class UIViewController;
class UIWindow: public UIView {
public:
UIWindow();
~UIWindow();

std::shared_ptr<UIWindow> window() override;
// std::shared_ptr<UIFocusSystem> focusSystem() { return _focusSystem; }

void setRootViewController(std::shared_ptr<UIViewController> controller);
std::shared_ptr<UIViewController> rootViewController() { return _rootViewController; }

void makeKeyAndVisible();
// void sendEvent(const std::shared_ptr<UIEvent>& event);

void layoutSubviews() override;
// void updateFocus();

// virtual void pressesBegan(std::set<std::shared_ptr<UIPress>> pressees, std::shared_ptr<UIPressesEvent> event) override;
// virtual void pressesChanged(std::set<std::shared_ptr<UIPress>> pressees, std::shared_ptr<UIPressesEvent> event) override;
// 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;

private:
std::shared_ptr<UIViewController> _rootViewController;
std::vector<std::shared_ptr<UIViewController>> _presentedViewControllers;

friend class UIViewController;
void addPresentedViewController(const std::shared_ptr<UIViewController>& controller);
void removePresentedViewController(const std::shared_ptr<UIViewController>& controller);

// void sendTouchEvent(std::shared_ptr<UIEvent> event);
// void sendPressEvent(const std::shared_ptr<UIPressesEvent>& event);

// std::shared_ptr<UIFocusSystem> _focusSystem;
};

}
}
Loading

0 comments on commit b7557f6

Please sign in to comment.