Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Gas Mask.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
AA000016000000000000AAAA /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA000015000000000000AAAA /* SidebarView.swift */; };
AA000018000000000000AAAA /* SidebarInstaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA000017000000000000AAAA /* SidebarInstaller.swift */; };
AA00001A000000000000AAAA /* HostsDataStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA000019000000000000AAAA /* HostsDataStoreTests.swift */; };
AA00001C000000000000AAAA /* HostsTextViewRepresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00001B000000000000AAAA /* HostsTextViewRepresentable.swift */; };
AA00001E000000000000AAAA /* CombinedHostsPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00001D000000000000AAAA /* CombinedHostsPickerView.swift */; };
AA000020000000000000AAAA /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00001F000000000000AAAA /* ContentView.swift */; };
AA000022000000000000AAAA /* ContentInstaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA000021000000000000AAAA /* ContentInstaller.swift */; };
BB000002000000000000BBBB /* RemoteIntervalMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB000001000000000000BBBB /* RemoteIntervalMapper.swift */; };
BB000004000000000000BBBB /* ShortcutRecorderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB000003000000000000BBBB /* ShortcutRecorderView.swift */; };
CC000002000000000000CC01 /* GlobalShortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC000001000000000000CC01 /* GlobalShortcuts.swift */; };
Expand Down Expand Up @@ -339,6 +343,10 @@
AA000015000000000000AAAA /* SidebarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SidebarView.swift; path = "Source/Swift/SidebarView.swift"; sourceTree = "<group>"; };
AA000017000000000000AAAA /* SidebarInstaller.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SidebarInstaller.swift; path = "Source/Swift/SidebarInstaller.swift"; sourceTree = "<group>"; };
AA000019000000000000AAAA /* HostsDataStoreTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HostsDataStoreTests.swift; sourceTree = "<group>"; };
AA00001B000000000000AAAA /* HostsTextViewRepresentable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = HostsTextViewRepresentable.swift; path = "Source/Swift/HostsTextViewRepresentable.swift"; sourceTree = "<group>"; };
AA00001D000000000000AAAA /* CombinedHostsPickerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CombinedHostsPickerView.swift; path = "Source/Swift/CombinedHostsPickerView.swift"; sourceTree = "<group>"; };
AA00001F000000000000AAAA /* ContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContentView.swift; path = "Source/Swift/ContentView.swift"; sourceTree = "<group>"; };
AA000021000000000000AAAA /* ContentInstaller.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContentInstaller.swift; path = "Source/Swift/ContentInstaller.swift"; sourceTree = "<group>"; };
35A183A71A0ACF37002D6289 /* menuIcon@2x.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; name = "menuIcon@2x.tiff"; path = "Resources/Images/menuIcon@2x.tiff"; sourceTree = "<group>"; };
BB000001000000000000BBBB /* RemoteIntervalMapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RemoteIntervalMapper.swift; path = "Source/Swift/RemoteIntervalMapper.swift"; sourceTree = "<group>"; };
BB000003000000000000BBBB /* ShortcutRecorderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ShortcutRecorderView.swift; path = "Source/Swift/ShortcutRecorderView.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -782,6 +790,10 @@
AA000013000000000000AAAA /* HostsRowView.swift */,
AA000015000000000000AAAA /* SidebarView.swift */,
AA000017000000000000AAAA /* SidebarInstaller.swift */,
AA00001B000000000000AAAA /* HostsTextViewRepresentable.swift */,
AA00001D000000000000AAAA /* CombinedHostsPickerView.swift */,
AA00001F000000000000AAAA /* ContentView.swift */,
AA000021000000000000AAAA /* ContentInstaller.swift */,
BB000020000000000000BBBB /* Preferences */,
);
name = Swift;
Expand Down Expand Up @@ -1110,6 +1122,10 @@
AA000014000000000000AAAA /* HostsRowView.swift in Sources */,
AA000016000000000000AAAA /* SidebarView.swift in Sources */,
AA000018000000000000AAAA /* SidebarInstaller.swift in Sources */,
AA00001C000000000000AAAA /* HostsTextViewRepresentable.swift in Sources */,
AA00001E000000000000AAAA /* CombinedHostsPickerView.swift in Sources */,
AA000020000000000000AAAA /* ContentView.swift in Sources */,
AA000022000000000000AAAA /* ContentInstaller.swift in Sources */,
BB000002000000000000BBBB /* RemoteIntervalMapper.swift in Sources */,
BB000004000000000000BBBB /* ShortcutRecorderView.swift in Sources */,
CC000002000000000000CC01 /* GlobalShortcuts.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Source/EditorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (void)awakeFromNib
}

[SidebarInstaller installIn:splitView];
[ContentInstaller installIn:splitView];
}

#pragma mark - Split View Delegate
Expand Down
2 changes: 2 additions & 0 deletions Source/HostsTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@
- (void)setSyntaxHighlighting:(BOOL)value;
- (BOOL)syntaxHighlighting;

+ (instancetype)createForProgrammaticUse;

@end
46 changes: 33 additions & 13 deletions Source/HostsTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,46 @@ -(NSRange)selectRangeFromDoubleClick:(NSUInteger)location range:(NSRange)range;

@implementation HostsTextView

- (id)initWithCoder:(NSCoder *)decoder
- (void)setupColors
{
self = [super initWithCoder:decoder];

ipv4Color = [NSColor colorWithCalibratedRed:0.27 green:0.36 blue:0.61 alpha:1];
ipv6Color = [NSColor colorWithCalibratedRed:0.27 green:0.36 blue:0.8 alpha:1];

if (@available(macOS 10_13, *)) {
textColor = [NSColor colorNamed:@"TextColor"];
commentColor = [NSColor colorNamed:@"CommentColor"];
} else {
textColor = [NSColor blackColor];
commentColor = [NSColor grayColor];
}

textColor = [NSColor colorNamed:@"TextColor"];
commentColor = [NSColor colorNamed:@"CommentColor"];
nameCharacterSet = [NSCharacterSet characterSetWithCharactersInString: @"abcdefghijklmnopqrstuvwxyz0123456789.-"];

}

- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
[self setupColors];
return self;
}

+ (instancetype)createForProgrammaticUse
{
NSTextContainer *container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[container setWidthTracksTextView:NO];

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:container];

NSTextStorage *textStorage = [[NSTextStorage alloc] init];
[textStorage addLayoutManager:layoutManager];

HostsTextView *textView = [[HostsTextView alloc] initWithFrame:NSZeroRect textContainer:container];
[textView setupColors];

[textStorage setDelegate:textView];
[textView setHorizontallyResizable:YES];
[textView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[textView setFont:[NSFont monospacedSystemFontOfSize:12 weight:NSFontWeightMedium]];
[textView setBackgroundColor:[NSColor colorNamed:@"BackgroundColor"]];
[textView setTextColor:[NSColor colorNamed:@"TextColor"]];

return textView;
}

- (void)awakeFromNib
{
[[super textStorage] setDelegate:self];
Expand Down
79 changes: 79 additions & 0 deletions Source/Swift/CombinedHostsPickerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import SwiftUI

struct CombinedHostsPickerView: View {
@ObservedObject var store: HostsDataStore

var body: some View {
if let combined = store.selectedHosts as? CombinedHosts {
pickerContent(for: combined)
}
}

// MARK: - Picker Content

@ViewBuilder
private func pickerContent(for combined: CombinedHosts) -> some View {
let allFiles = allNonCombinedHosts()
let includedFiles = (combined.hostsFiles() as? [Hosts]) ?? []

ScrollView {
VStack(alignment: .leading, spacing: 2) {
if allFiles.isEmpty {
Text("No hosts files available to combine.")
.foregroundStyle(.secondary)
.font(.system(size: NSFont.smallSystemFontSize))
.padding(.horizontal, 8)
} else {
ForEach(allFiles, id: \.self) { hosts in
Toggle(
hosts.name() ?? "",
isOn: toggleBinding(for: hosts, in: combined, includedFiles: includedFiles)
)
.toggleStyle(.checkbox)
.font(.system(size: NSFont.smallSystemFontSize))
.padding(.horizontal, 8)
.padding(.vertical, 1)
}
}
}
.padding(.vertical, 4)
}
.frame(maxHeight: 150)
}

// MARK: - Toggle Binding

private func toggleBinding(for hosts: Hosts, in combined: CombinedHosts, includedFiles: [Hosts]) -> Binding<Bool> {
Binding(
get: {
let current = (combined.hostsFiles() as? [Hosts]) ?? []
return current.contains { $0 === hosts }
},
set: { isOn in
var files = (combined.hostsFiles() as? [Hosts]) ?? []
if isOn {
if !files.contains(where: { $0 === hosts }) {
files.append(hosts)
}
} else {
files.removeAll { $0 === hosts }
}
combined.setHostsFiles(files)
store.hostsGroups = store.hostsGroups
}
)
}

// MARK: - Helpers

private func allNonCombinedHosts() -> [Hosts] {
var result: [Hosts] = []
for group in store.hostsGroups {
let children = (group.children as? [Hosts]) ?? []
for hosts in children where !(hosts is CombinedHosts) {
result.append(hosts)
}
}
return result
}
}
16 changes: 16 additions & 0 deletions Source/Swift/ContentInstaller.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import SwiftUI

@objc final class ContentInstaller: NSObject {
@objc static func install(in splitView: NSSplitView) {
guard splitView.subviews.count >= 2 else { return }

let store = HostsDataStore.shared
let content = ContentView(store: store)
let hostingView = NSHostingView(rootView: content)

let rightPane = splitView.subviews[1]
hostingView.frame = rightPane.frame
hostingView.autoresizingMask = [.width, .height]
splitView.replaceSubview(rightPane, with: hostingView)
}
}
15 changes: 15 additions & 0 deletions Source/Swift/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import SwiftUI

struct ContentView: View {
@ObservedObject var store: HostsDataStore

var body: some View {
VStack(spacing: 0) {
if store.selectedHosts is CombinedHosts {
CombinedHostsPickerView(store: store)
Divider()
}
HostsTextViewRepresentable(store: store)
}
}
}
1 change: 1 addition & 0 deletions Source/Swift/GasMask-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
#import "LocalHostsController.h"
#import "CombinedHostsController.h"
#import "ListController.h"
#import "HostsTextView.h"
#import "Preferences+Remote.h"
#import "LoginItem.h"
62 changes: 62 additions & 0 deletions Source/Swift/HostsTextViewRepresentable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import SwiftUI

struct HostsTextViewRepresentable: NSViewRepresentable {
@ObservedObject var store: HostsDataStore
@AppStorage("syntaxHighlighting") private var syntaxHighlighting = true

func makeNSView(context: Context) -> NSScrollView {
guard let textView = HostsTextView.createForProgrammaticUse() else {
return NSScrollView()
}
textView.delegate = context.coordinator
textView.setSyntaxHighlighting(syntaxHighlighting)

let scrollView = NSScrollView()
scrollView.documentView = textView
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.drawsBackground = false

context.coordinator.textView = textView
return scrollView
}

func updateNSView(_ scrollView: NSScrollView, context: Context) {
guard let textView = context.coordinator.textView else { return }

let contents = store.selectedHosts?.contents() ?? ""
if textView.string != contents {
context.coordinator.isUpdatingFromModel = true
textView.string = contents
context.coordinator.isUpdatingFromModel = false
}

textView.isEditable = store.selectedHosts?.editable ?? false

if textView.syntaxHighlighting() != syntaxHighlighting {
textView.setSyntaxHighlighting(syntaxHighlighting)
}
}

func makeCoordinator() -> Coordinator {
Coordinator(store: store)
}

// MARK: - Coordinator

@MainActor final class Coordinator: NSObject, NSTextViewDelegate {
let store: HostsDataStore
weak var textView: HostsTextView?
var isUpdatingFromModel = false

init(store: HostsDataStore) {
self.store = store
}

func textDidChange(_ notification: Notification) {
guard !isUpdatingFromModel,
let textView = notification.object as? HostsTextView else { return }
store.selectedHosts?.setContents(textView.string)
}
}
}