Skip to content

Commit

Permalink
Now handling back in webview, can copy current url, qol improvements …
Browse files Browse the repository at this point in the history
…for bookmarks, and more
  • Loading branch information
curzel-it committed Aug 7, 2022
1 parent ec44e9e commit 2b079cf
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 107 deletions.
8 changes: 8 additions & 0 deletions Pipper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
1E93EECB289668F3006CF96A /* BookmarkEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E93EECA289668F3006CF96A /* BookmarkEditor.swift */; };
1E93EECD289687CD006CF96A /* MouseHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E93EECC289687CD006CF96A /* MouseHandler.swift */; };
1E9497EE289ABC54004E76D9 /* StorageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E9497ED289ABC54004E76D9 /* StorageService.swift */; };
1EA3930D289D880F004E78E1 /* Browser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EA3930C289D880F004E78E1 /* Browser.swift */; };
1EC926B7289F98BD0056E42C /* URLUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EC926B6289F98BD0056E42C /* URLUtils.swift */; };
1ED28C9E2892BC1100D35DA4 /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ED28C9D2892BC1100D35DA4 /* Models.swift */; };
1ED28CA02892C4A200D35DA4 /* WindowManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ED28C9F2892C4A200D35DA4 /* WindowManager.swift */; };
1ED28CA22892CF3700D35DA4 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ED28CA12892CF3700D35DA4 /* Search.swift */; };
Expand Down Expand Up @@ -78,6 +80,8 @@
1E93EECA289668F3006CF96A /* BookmarkEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarkEditor.swift; sourceTree = "<group>"; };
1E93EECC289687CD006CF96A /* MouseHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MouseHandler.swift; sourceTree = "<group>"; };
1E9497ED289ABC54004E76D9 /* StorageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageService.swift; sourceTree = "<group>"; };
1EA3930C289D880F004E78E1 /* Browser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Browser.swift; sourceTree = "<group>"; };
1EC926B6289F98BD0056E42C /* URLUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLUtils.swift; sourceTree = "<group>"; };
1ED0A57D2893C3A200272CEB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
1ED28C9D2892BC1100D35DA4 /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
1ED28C9F2892C4A200D35DA4 /* WindowManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -177,6 +181,7 @@
1E2EBC3E289282180008ECB3 /* AppState.swift */,
1E93EECA289668F3006CF96A /* BookmarkEditor.swift */,
1E93EEC52895D84E006CF96A /* Bookmarks.swift */,
1EA3930C289D880F004E78E1 /* Browser.swift */,
1E2EBC40289282580008ECB3 /* Colors.swift */,
1E2EBC3C289281FF0008ECB3 /* ContentView.swift */,
1E93EEC72895D941006CF96A /* Forms.swift */,
Expand All @@ -188,6 +193,7 @@
1E2EBC3A289280670008ECB3 /* Settings.swift */,
1E9497ED289ABC54004E76D9 /* StorageService.swift */,
1E2EBC3728927D650008ECB3 /* Toolbar.swift */,
1EC926B6289F98BD0056E42C /* URLUtils.swift */,
1E93EEC12895D1E2006CF96A /* UserMessages.swift */,
1E3AFC992891C25100C6A465 /* WebView.swift */,
1E3AFC9B2891C7C200C6A465 /* WebViewDelegate.swift */,
Expand Down Expand Up @@ -361,6 +367,7 @@
files = (
1ED28CA22892CF3700D35DA4 /* Search.swift in Sources */,
1E93EEC62895D84E006CF96A /* Bookmarks.swift in Sources */,
1EA3930D289D880F004E78E1 /* Browser.swift in Sources */,
1E3AFC9A2891C25100C6A465 /* WebView.swift in Sources */,
1E2EBC41289282580008ECB3 /* Colors.swift in Sources */,
1E93EEBD2895BE84006CF96A /* Homepage.swift in Sources */,
Expand All @@ -372,6 +379,7 @@
1E2EBC3D289281FF0008ECB3 /* ContentView.swift in Sources */,
1E2EBC3F289282180008ECB3 /* AppState.swift in Sources */,
1E2EBC3828927D650008ECB3 /* Toolbar.swift in Sources */,
1EC926B7289F98BD0056E42C /* URLUtils.swift in Sources */,
1ED28C9E2892BC1100D35DA4 /* Models.swift in Sources */,
1E93EECB289668F3006CF96A /* BookmarkEditor.swift in Sources */,
1E2EBC3B289280670008ECB3 /* Settings.swift in Sources */,
Expand Down
15 changes: 14 additions & 1 deletion Pipper/Sources/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ class AppState: ObservableObject {

@Published var isHovering = true
@Published var userMessage: UserMessage?
@Published var showAdditionalInfo = true
@Published var showHome = true
@Published var showSearch = false
@Published var showSettings = false
@Published var navigationRequest: NavigationRequest = .urlString(urlString: "about:blank")
@Published var vistedUrlsStack: [URL] = []
@Published var isLoading = false

@Published private(set) var navigationRequest: NavigationRequest = .urlString(
urlString: "about:blank"
)

let runtimeEvents = CurrentValueSubject<RuntimeEvent, Never>(.loading)

Expand All @@ -26,6 +32,13 @@ class AppState: ObservableObject {
lazy var windowManager: WindowManager = {
WindowManager(appState: self)
}()

func load(_ request: NavigationRequest) {
self.showHome = false
self.showSettings = false
self.showSearch = false
self.navigationRequest = request
}
}

enum RuntimeEvent {
Expand Down
2 changes: 1 addition & 1 deletion Pipper/Sources/Bookmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private struct BookmarkItem: View {
func visit() {
withAnimation {
appState.showHome = false
appState.navigationRequest = .urlString(urlString: bookmark.url)
appState.load(.urlString(urlString: bookmark.url))
}
}

Expand Down
54 changes: 54 additions & 0 deletions Pipper/Sources/Browser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// WebViewInsights.swift
// Pipper
//
// Created by Federico Curzel on 05/08/22.
//

import Combine
import SwiftUI

struct Browser: View {

@EnvironmentObject var appState: AppState

var body: some View {
VStack(spacing: 0) {
WebView()
if appState.showAdditionalInfo {
VStack(spacing: 0) {
Rectangle().fill(Color.tertiaryLabel, style: .init()).frame(height: 1)
UrlBar()
}
}
}
}
}

private struct UrlBar: View {

@EnvironmentObject var appState: AppState

var currentUrl: String {
appState.vistedUrlsStack.last?.absoluteString ?? ""
}

var body: some View {
HStack(spacing: 12) {
if appState.isLoading {
ProgressView()
.progressViewStyle(.circular)
.scaleEffect(0.6)
}
Text(currentUrl)
.font(.caption)
.lineLimit(2)
.onTapGesture {
let data = currentUrl.data(using: .utf8)
NSPasteboard.general.setData(data, forType: .string)
appState.userMessage = .init(text: "Url Copied!", duracy: .short, severity: .info)
}
}
.padding(8)
}
}
5 changes: 3 additions & 2 deletions Pipper/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ struct ContentView: View {
ZStack {
Homepage()
.opacity(appState.showHome ? 1 : 0)
// Browser()
WebView()
.opacity(appState.showHome ? 0 : 1)

if appState.showSearch { Search() }
if appState.showSearch { SearchBar() }
UserMessages()
}
Toolbar()
if appState.showAdditionalInfo { Toolbar() }
}
.sheet(isPresented: $appState.showSettings) {
SettingsView()
Expand Down
12 changes: 12 additions & 0 deletions Pipper/Sources/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ struct Bookmark: Codable, Identifiable, Equatable {
let url: String
let icon: String
}

extension URL {

func asBookmark(title: String) -> Bookmark {
Bookmark(
id: UUID().uuidString,
title: title,
url: absoluteString,
icon: favicon?.absoluteString ?? absoluteString
)
}
}
1 change: 0 additions & 1 deletion Pipper/Sources/MyApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ struct PipperApp: App {
ContentView()
.environmentObject(storage)
}
.windowStyle(.hiddenTitleBar)
}
}
67 changes: 3 additions & 64 deletions Pipper/Sources/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Combine
import Foundation
import SwiftUI

struct Search: View {
struct SearchBar: View {

@EnvironmentObject var appState: AppState
@EnvironmentObject var storage: StorageService
Expand All @@ -26,22 +26,19 @@ struct Search: View {
.onSubmit(searchOrVisit)
.focused($focused)
.onAppear { focused = true }

SearchCurrentText(action: searchOrVisit)
CloseSearchBar()
VisitSearchEngineHome()
}
.padding(.trailing)
.background(Color.secondaryBackground)
.cornerRadius(8)
.shadow(radius: 16)
.padding(.horizontal, 40)
.onSubmit(searchOrVisit)
.onExitCommand(perform: close)
}

private func searchOrVisit() {
appState.showHome = false
appState.navigationRequest = .search(input: text)
appState.load(.search(input: text))
close()
}

Expand All @@ -51,61 +48,3 @@ struct Search: View {
}
}
}

private struct SearchCurrentText: View {

@EnvironmentObject var appState: AppState
@EnvironmentObject var storage: StorageService

let action: () -> Void

var body: some View {
Button(action: action) {
Image(systemName: "magnifyingglass")
}
.keyboardShortcut(.return)
.onHover(hint: "RETURN 􀅇\nSearches your input")
}
}

private struct CloseSearchBar: View {

@EnvironmentObject var appState: AppState
@EnvironmentObject var storage: StorageService

var body: some View {
Button(action: close) {
Image(systemName: "xmark")
}
.keyboardShortcut(.cancelAction)
.onHover(hint: "ESC 􀆧\nCloses the search bar")
}

func close() {
withAnimation {
appState.showSearch = false
}
}
}

private struct VisitSearchEngineHome: View {

@EnvironmentObject var appState: AppState
@EnvironmentObject var storage: StorageService

var body: some View {
Button(action: visitSearchEngineHome) {
Image(systemName: "arrow.up.left.and.arrow.down.right")
}
.keyboardShortcut(.init("E"), modifiers: [.command, .shift])
.onHover(hint: "CMD + Shift + E\nOpen Search Engine homepage")
}

func visitSearchEngineHome() {
withAnimation {
appState.showSearch = false
appState.showHome = false
appState.navigationRequest = .urlString(urlString: storage.searchEngineBaseUrl)
}
}
}
9 changes: 9 additions & 0 deletions Pipper/Sources/StorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ extension StorageService {
saveBookmarks()
}

func remove(bookmark: URL) {
bookmarks = bookmarks.filter { $0.url != bookmark.absoluteString }
saveBookmarks()
}

func isBookmarked(_ url: URL) -> Bool {
bookmarks.contains { $0.url == url.absoluteString }
}

fileprivate func loadBookmarks() {
if let data = storedBookmarks,
let value = try? JSONDecoder().decode([Bookmark].self, from: data) {
Expand Down
Loading

0 comments on commit 2b079cf

Please sign in to comment.