From 63b8ea2ecd236dcc391418f7afd5182cd12352ab Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Sat, 28 Nov 2020 14:19:20 +0100 Subject: [PATCH] Refactor slightly --- .../Tabs/{Dimensions.swift => Defs.swift} | 5 +++-- Tabs/Sources/Tabs/Tab.swift | 17 +++++++++++++++-- Tabs/Sources/Tabs/TabBar.swift | 10 +++++----- Tabs/Support/TabsSupport/AppDelegate.swift | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-) rename Tabs/Sources/Tabs/{Dimensions.swift => Defs.swift} (77%) diff --git a/Tabs/Sources/Tabs/Dimensions.swift b/Tabs/Sources/Tabs/Defs.swift similarity index 77% rename from Tabs/Sources/Tabs/Dimensions.swift rename to Tabs/Sources/Tabs/Defs.swift index a3b4ff0f..73b80277 100644 --- a/Tabs/Sources/Tabs/Dimensions.swift +++ b/Tabs/Sources/Tabs/Defs.swift @@ -3,12 +3,13 @@ * See LICENSE */ -import Foundation +import Cocoa -public enum Dimensions { +public enum Defs { public static let tabHeight = CGFloat(28) public static let tabMinWidth = CGFloat(100) public static let tabMaxWidth = CGFloat(400) + public static let tabTitleFont = NSFont.systemFont(ofSize: 11) public static let tabPadding = CGFloat(0) diff --git a/Tabs/Sources/Tabs/Tab.swift b/Tabs/Sources/Tabs/Tab.swift index 0a446268..e898187b 100644 --- a/Tabs/Sources/Tabs/Tab.swift +++ b/Tabs/Sources/Tabs/Tab.swift @@ -6,9 +6,16 @@ import Cocoa public class Tab: NSView { + + public var title: String - public init() { + public init(withTitle title: String) { + self.title = title + self.attributedTitle = NSAttributedString(string: title, attributes: [ + .font: Defs.tabTitleFont + ]) super.init(frame: .zero) + self.configureForAutoLayout() self.wantsLayer = true @@ -16,7 +23,7 @@ public class Tab: NSView { self.layer?.backgroundColor = NSColor.cyan.cgColor #endif - self.autoSetDimensions(to: CGSize(width: 200, height: Dimensions.tabHeight)) + self.autoSetDimensions(to: CGSize(width: 200, height: Defs.tabHeight)) } public override func mouseDown(with event: NSEvent) { @@ -27,6 +34,12 @@ public class Tab: NSView { Swift.print("mouse up in tab") } + public override func draw(_ dirtyRect: NSRect) { + self.attributedTitle.draw(in: self.bounds) + } + @available(*, unavailable) required init?(coder _: NSCoder) { fatalError("init(coder:) has not been implemented") } + + private var attributedTitle: NSAttributedString } diff --git a/Tabs/Sources/Tabs/TabBar.swift b/Tabs/Sources/Tabs/TabBar.swift index b4231039..8d69f962 100644 --- a/Tabs/Sources/Tabs/TabBar.swift +++ b/Tabs/Sources/Tabs/TabBar.swift @@ -44,14 +44,14 @@ public class TabBar: NSView { self.stackView.autoPinEdge(toSuperviewEdge: .left) self.stackView.autoPinEdge(toSuperviewEdge: .bottom) - self.stackView.spacing = Dimensions.tabPadding + self.stackView.spacing = Defs.tabPadding } private func addTestTabs() { - let tab1 = Tab() - let tab2 = Tab() - let tab3 = Tab() - let tab4 = Tab() + let tab1 = Tab(withTitle: "Test 1") + let tab2 = Tab(withTitle: "Test 2") + let tab3 = Tab(withTitle: "Test 3") + let tab4 = Tab(withTitle: "Test 4") tab1.layer?.backgroundColor = NSColor.red.cgColor tab2.layer?.backgroundColor = NSColor.blue.cgColor diff --git a/Tabs/Support/TabsSupport/AppDelegate.swift b/Tabs/Support/TabsSupport/AppDelegate.swift index 6d21c933..dc660bda 100644 --- a/Tabs/Support/TabsSupport/AppDelegate.swift +++ b/Tabs/Support/TabsSupport/AppDelegate.swift @@ -24,7 +24,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.tabBar.autoPinEdge(toSuperviewEdge: .top) self.tabBar.autoPinEdge(toSuperviewEdge: .left) self.tabBar.autoPinEdge(toSuperviewEdge: .right) - self.tabBar.autoSetDimension(.height, toSize: Dimensions.tabBarHeight) + self.tabBar.autoSetDimension(.height, toSize: Defs.tabBarHeight) } func applicationWillTerminate(_: Notification) {