Skip to content

Commit

Permalink
sizeToFit impl
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Dec 16, 2024
1 parent 58675d1 commit 5f76374
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Submodules/UIKit/include/CATextLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CATextLayer: public CALayer {
[[nodiscard]] NSTextAlignment textAlignment() const { return _textAlignment; }

std::optional<AnimatableProperty> value(std::string forKeyPath) override;

NXSize sizeThatFits(NXSize size);
protected:
void update(std::shared_ptr<CALayer> presentation, std::shared_ptr<CABasicAnimation> animation, float progress) override;

Expand Down
4 changes: 2 additions & 2 deletions Submodules/UIKit/include/UIImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class UIImageView: public UIView {
std::shared_ptr<UIImage> image() { return _image; }

// void sizeToFit() override;
// Size sizeThatFits(Size size) override;
NXSize sizeThatFits(NXSize size) override;
// bool applyXMLAttribute(std::string name, std::string value) override;

private:
std::shared_ptr<UIImage> _image;
void updateTextureFromImage();
};

}
}
2 changes: 2 additions & 0 deletions Submodules/UIKit/include/UILabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class UILabel: public UIView {
void setFontWeight(NXFloat fontWeight) { _textLayer()->setFontWeight(fontWeight); }
[[nodiscard]] NXFloat fontWeight() const { return _textLayer()->fontWeight(); }

NXSize sizeThatFits(NXSize size) override;

private:
int _numberOfLines = 1;
std::shared_ptr<CATextLayer> _textLayer() const;
Expand Down
2 changes: 1 addition & 1 deletion Submodules/UIKit/include/UIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UIView: public CALayerDelegate, public enable_shared_from_this<UIView> {
virtual void layoutSubviews();

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

void drawAndLayoutTreeIfNeeded();

Expand Down
8 changes: 8 additions & 0 deletions Submodules/UIKit/lib/CATextLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ void CATextLayer::update(std::shared_ptr<CALayer> presentation, std::shared_ptr<

CALayer::update(presentation, animation, progress);
}

NXSize CATextLayer::sizeThatFits(NXSize size) {
paragraph->layout(size.width);
auto height = paragraph->getHeight();
auto width = paragraph->getMaxIntrinsicWidth();
auto rWidth = std::ceil(width);
return { rWidth, height };
}
8 changes: 4 additions & 4 deletions Submodules/UIKit/lib/UIImageView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void UIImageView::updateTextureFromImage() {
// UIView::sizeToFit();
// }

// Size UIImageView::sizeThatFits(Size size) {
// if (!_image) return Size();
// return _image->size();
// }
NXSize UIImageView::sizeThatFits(NXSize size) {
if (!_image) return UIView::sizeThatFits(size);
return _image->size();
}

// bool UIImageView::applyXMLAttribute(std::string name, std::string value) {
// if (UIView::applyXMLAttribute(name, value)) return true;
Expand Down
4 changes: 4 additions & 0 deletions Submodules/UIKit/lib/UILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ UILabel::UILabel(): UIView(NXRect(), new_shared<CATextLayer>()) {}
std::shared_ptr<CATextLayer> UILabel::_textLayer() const {
return std::static_pointer_cast<CATextLayer>(layer());
}

NXSize UILabel::sizeThatFits(NXSize size) {
return _textLayer()->sizeThatFits(size);
}
8 changes: 7 additions & 1 deletion Submodules/UIKit/lib/UIView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,13 @@ NXSize UIView::sizeThatFits(NXSize size) {
}

void UIView::sizeToFit() {
auto bounds = this->bounds();
NXRect bounds;
if (!superview().expired()) {
bounds = this->superview().lock()->bounds();
} else {
bounds = this->bounds();
}

bounds.size = sizeThatFits(bounds.size);
setBounds(bounds);
}
8 changes: 8 additions & 0 deletions app/Screens/TestViewController/TestViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ void TestViewController::loadView() {
bottomBar->setBackgroundColor(UIColor::gray);
rootView->addSubview(bottomBar);

label2 = new_shared<UILabel>();
label2->setText("Test text\nTry to fit me!!!");
label2->setBackgroundColor(UIColor::cyan);
label2->setFrame({ 100, 200, 0, 0 });
rootView->addSubview(label2);

setView(rootView);
}

Expand All @@ -115,6 +121,8 @@ void TestViewController::viewDidLoad() {
void TestViewController::viewDidLayoutSubviews() {
UIViewController::viewDidLayoutSubviews();

label2->sizeToFit();

auto frame = view()->frame();
NXFloat bottomBarHeight = 48 + 35; //83;
bottomBar->setFrame({ 0, frame.size.height - bottomBarHeight, frame.size.width, bottomBarHeight });
Expand Down
1 change: 1 addition & 0 deletions app/Screens/TestViewController/TestViewController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class TestViewController: public NXKit::UIViewController {
void viewDidLayoutSubviews() override;
private:
std::shared_ptr<NXKit::UILabel> label;
std::shared_ptr<NXKit::UILabel> label2;
std::shared_ptr<NXKit::UIView> bottomBar;
};

0 comments on commit 5f76374

Please sign in to comment.