uicore-ts
Version:
UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha
275 lines (274 loc) • 10.1 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var UIButton_exports = {};
__export(UIButton_exports, {
UIButton: () => UIButton
});
module.exports = __toCommonJS(UIButton_exports);
var import_UIBaseButton = require("./UIBaseButton");
var import_UIColor = require("./UIColor");
var import_UIImageView = require("./UIImageView");
var import_UIObject = require("./UIObject");
var import_UIRectangle = require("./UIRectangle");
var import_UITextView = require("./UITextView");
class UIButton extends import_UIBaseButton.UIBaseButton {
constructor(elementID, elementType, titleType = import_UITextView.UITextView.type.span) {
super(elementID, elementType);
this._contentPadding = 0;
this._titleLabel = import_UIObject.nil;
this.usesAutomaticTitleFontSize = import_UIObject.NO;
this.maxAutomaticFontSize = 25;
this.colors = {
titleLabel: {
normal: import_UIColor.UIColor.whiteColor,
highlighted: import_UIColor.UIColor.whiteColor,
selected: import_UIColor.UIColor.whiteColor
},
background: {
normal: import_UIColor.UIColor.blueColor,
highlighted: import_UIColor.UIColor.greenColor,
selected: import_UIColor.UIColor.redColor
}
};
this._imageView = new import_UIImageView.UIImageView(this.elementID + "ImageView");
this._imageView.hidden = import_UIObject.YES;
this.addSubview(this.imageView);
this.imageView.fillMode = import_UIImageView.UIImageView.fillMode.aspectFitIfLarger;
if ((0, import_UIObject.IS_NOT_NIL)(titleType)) {
this._titleLabel = new import_UITextView.UITextView(this.elementID + "TitleLabel", titleType);
this.titleLabel.style.whiteSpace = "nowrap";
this.addSubview(this.titleLabel);
this.titleLabel.userInteractionEnabled = import_UIObject.NO;
}
this.contentPadding = 10;
this.imageView.userInteractionEnabled = import_UIObject.NO;
if (this.titleLabel) {
this.titleLabel.textAlignment = import_UITextView.UITextView.textAlignment.center;
this.titleLabel.nativeSelectionEnabled = import_UIObject.NO;
}
}
get contentPadding() {
return this._contentPadding.integerValue;
}
set contentPadding(contentPadding) {
this._contentPadding = contentPadding;
this.setNeedsLayout();
}
set hovered(hovered) {
this._hovered = hovered;
this.updateContentForCurrentState();
}
get hovered() {
var _a;
return (_a = this._hovered) != null ? _a : import_UIObject.NO;
}
set highlighted(highlighted) {
this._highlighted = highlighted;
this.updateContentForCurrentState();
}
get highlighted() {
return this._highlighted;
}
set focused(focused) {
this._focused = focused;
if (focused) {
this.focus();
} else {
this.blur();
}
this.updateContentForCurrentState();
}
get focused() {
var _a;
return (_a = this._focused) != null ? _a : import_UIObject.NO;
}
set selected(selected) {
this._selected = selected;
this.updateContentForCurrentState();
}
get selected() {
return this._selected;
}
updateContentForCurrentState() {
let updateFunction = this.updateContentForNormalState;
if (this.selected && this.highlighted) {
updateFunction = this.updateContentForSelectedAndHighlightedState;
} else if (this.selected) {
updateFunction = this.updateContentForSelectedState;
} else if (this.focused) {
updateFunction = this.updateContentForFocusedState;
} else if (this.highlighted) {
updateFunction = this.updateContentForHighlightedState;
} else if (this.hovered) {
updateFunction = this.updateContentForHoveredState;
}
if (!(0, import_UIObject.IS)(updateFunction)) {
if (this.titleLabel) {
this.titleLabel.textColor = import_UIColor.UIColor.nilColor;
}
this.backgroundColor = import_UIColor.UIColor.nilColor;
} else {
updateFunction.call(this);
}
this.updateContentForCurrentEnabledState();
}
updateContentForNormalState() {
this.backgroundColor = this.colors.background.normal;
if (this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.normal;
}
}
updateContentForHoveredState() {
this.updateContentForNormalState();
if (this.colors.background.hovered) {
this.backgroundColor = this.colors.background.hovered;
}
if (this.colors.titleLabel.hovered && this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.hovered;
}
}
updateContentForFocusedState() {
this.updateContentForHoveredState();
if (this.colors.background.focused) {
this.backgroundColor = this.colors.background.focused;
}
if (this.colors.titleLabel.focused && this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.focused;
}
}
updateContentForHighlightedState() {
this.backgroundColor = this.colors.background.highlighted;
if (this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.highlighted;
}
}
updateContentForSelectedState() {
this.backgroundColor = this.colors.background.selected;
if (this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.selected;
}
}
updateContentForSelectedAndHighlightedState() {
this.updateContentForSelectedState();
if (this.colors.background.selectedAndHighlighted) {
this.backgroundColor = this.colors.background.selectedAndHighlighted;
}
if (this.colors.titleLabel.selectedAndHighlighted && this.titleLabel) {
this.titleLabel.textColor = this.colors.titleLabel.selectedAndHighlighted;
}
}
set enabled(enabled) {
super.enabled = enabled;
this.updateContentForCurrentState();
}
get enabled() {
return super.enabled;
}
updateContentForCurrentEnabledState() {
if (this.enabled) {
this.alpha = 1;
} else {
this.alpha = 0.5;
}
this.userInteractionEnabled = this.enabled;
}
addStyleClass(styleClassName) {
super.addStyleClass(styleClassName);
if (this.styleClassName != styleClassName) {
this.updateContentForCurrentState.call(this);
}
}
get titleLabel() {
return this._titleLabel;
}
get imageView() {
return this._imageView;
}
layoutSubviews() {
var _a, _b, _c, _d, _e;
super.layoutSubviews();
let bounds = this.bounds;
this.hoverText = (_b = (_a = this.titleLabel) == null ? void 0 : _a.text) != null ? _b : "";
if ((0, import_UIObject.IS_NOT)(this.imageView.hidden) && !(0, import_UIObject.IS)((_c = this.titleLabel) == null ? void 0 : _c.text)) {
this.imageView.frame = bounds;
}
if ((0, import_UIObject.IS)(this.imageView.hidden) && ((_d = this.titleLabel) == null ? void 0 : _d.text)) {
this.titleLabel.style.left = this.contentPadding + "px";
this.titleLabel.style.right = this.contentPadding + "px";
this.titleLabel.style.top = "50%";
this.titleLabel.style.transform = "translateY(-50%)";
this.titleLabel.frame = new import_UIRectangle.UIRectangle(import_UIObject.nil, import_UIObject.nil, import_UIObject.nil, import_UIObject.nil);
if (this.usesAutomaticTitleFontSize) {
const hidden = this.titleLabel.hidden;
this.titleLabel.hidden = import_UIObject.YES;
this.titleLabel.fontSize = 15;
this.titleLabel.fontSize = import_UITextView.UITextView.automaticallyCalculatedFontSize(
new import_UIRectangle.UIRectangle(
0,
0,
this.bounds.height,
this.titleLabel.viewHTMLElement.offsetWidth
),
this.titleLabel.intrinsicContentSize(),
this.titleLabel.fontSize,
this.minAutomaticFontSize,
this.maxAutomaticFontSize
);
this.titleLabel.hidden = hidden;
}
}
if ((0, import_UIObject.IS_NOT)(this.imageView.hidden) && ((_e = this.titleLabel) == null ? void 0 : _e.text)) {
bounds = bounds.rectangleWithInset(this.contentPadding);
const imageFrame = bounds.copy();
imageFrame.width = bounds.height - this.contentPadding * 0.5;
this.imageView.frame = imageFrame;
this.titleLabel.style.left = imageFrame.max.x + this.contentPadding + "px";
this.titleLabel.style.right = this.contentPadding + "px";
this.titleLabel.style.top = "50%";
this.titleLabel.style.transform = "translateY(-50%)";
if (this.usesAutomaticTitleFontSize) {
const hidden = this.titleLabel.hidden;
this.titleLabel.hidden = import_UIObject.YES;
this.titleLabel.fontSize = 15;
this.titleLabel.fontSize = import_UITextView.UITextView.automaticallyCalculatedFontSize(
new import_UIRectangle.UIRectangle(
0,
0,
this.bounds.height,
this.titleLabel.viewHTMLElement.offsetWidth
),
this.titleLabel.intrinsicContentSize(),
this.titleLabel.fontSize,
this.minAutomaticFontSize,
this.maxAutomaticFontSize
);
this.titleLabel.hidden = hidden;
}
}
this.applyClassesAndStyles();
}
initViewStyleSelectors() {
this.initStyleSelector("." + this.styleClassName, "background-color: lightblue;");
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
UIButton
});
//# sourceMappingURL=UIButton.js.map