Skip to content

Commit

Permalink
A little refactor, context menu items.
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Feb 1, 2024
1 parent 3ae4c7c commit 502e68b
Show file tree
Hide file tree
Showing 21 changed files with 467 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.ultreon.browser.main
package com.ultreon.browser

import com.formdev.flatlaf.FlatLaf
import com.formdev.flatlaf.FlatPropertiesLaf
import com.formdev.flatlaf.IntelliJTheme
import com.formdev.flatlaf.themes.FlatMacDarkLaf
import com.formdev.flatlaf.util.LoggingFacade
import com.formdev.flatlaf.util.StringUtils
import com.ultreon.browser.APP_ID
import com.ultreon.browser.util.APP_ID
import com.ultreon.browser.intellijthemes.IJThemesPanel
import java.beans.PropertyChangeEvent
import java.io.File
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.ultreon.browser.main
package com.ultreon.browser

import com.ultreon.browser.LOADING_ICON
import com.ultreon.browser.useOSR
import com.ultreon.browser.util.LOADING_ICON
import com.ultreon.browser.util.useOSR
import org.cef.CefClient
import org.cef.browser.CefBrowser
import java.awt.*
import java.awt.event.FocusAdapter
import java.awt.event.FocusEvent
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.File
import java.io.FileWriter
import java.net.URL
import java.net.URLEncoder
import javax.swing.*

class BrowserTab(val tabs: BrowserTabs, val icon: JLabel, client: CefClient, main: UltreonBrowser, val url: String, val openInBg: Boolean) : JPanel(CardLayout()) {
private var loading: Boolean = true
private var title: JLabel
private var btnClose: JButton
private var pnlTab: JPanel
Expand Down Expand Up @@ -115,7 +119,11 @@ class BrowserTab(val tabs: BrowserTabs, val icon: JLabel, client: CefClient, mai
}

fun goTo(url: String) = SwingUtilities.invokeLater {
browser.loadURL(url)
if (url.isValidURL()) {
browser.loadURL(url)
} else {
browser.loadURL("https://www.google.com/search?q=${url.encodeUrl()}")
}
}

fun goForward() = SwingUtilities.invokeLater {
Expand All @@ -127,14 +135,72 @@ class BrowserTab(val tabs: BrowserTabs, val icon: JLabel, client: CefClient, mai
}

fun updateIcon(image: Image) {
icon.icon = ImageIcon(image)
SwingUtilities.invokeLater {
icon.icon = ImageIcon(image)
}
}

fun loadStart() = SwingUtilities.invokeLater {
loading = true
icon.icon = ImageIcon(LOADING_ICON)
}

fun loadEnd() = SwingUtilities.invokeLater {
icon.icon = null
loading = false
}

fun savePage() {
browser.getSource { source ->
SwingUtilities.invokeLater {
saveSource(source)
}
}
}

private fun saveSource(source: String?) {
if (source == null) {
JOptionPane.showMessageDialog(UltreonBrowser.instance, "Failed to get source", "Error", JOptionPane.ERROR_MESSAGE)
return
}

val fileChooser = JFileChooser().apply {
fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
dialogTitle = "Save Page"
isAcceptAllFileFilterUsed = false
}

if (fileChooser.showSaveDialog(UltreonBrowser.instance) == JFileChooser.APPROVE_OPTION) {
val file = fileChooser.selectedFile
FileWriter(File(file, "index.html")).use {
it.write(source)
}
}
}

fun reload() {
if (loading) {
browser.stopLoad()
} else {
browser.reload()
}
}
}

private fun String.encodeUrl(): String {
return URLEncoder.encode(this, "UTF-8")
}

private fun String.isValidURL(): Boolean {
return try {
with(URL(this)) {
protocol == "http" || protocol == "https"
}
} catch (e: Exception) {
try {
URLEncoder.encode(this, "UTF-8") == this
} catch (e: Exception) {
false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.ultreon.browser.main
package com.ultreon.browser

import com.ultreon.browser.LOADING_ICON
import com.ultreon.browser.util.LOADING_ICON
import org.cef.CefClient
import org.cef.browser.CefBrowser
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.net.URL
import javax.swing.*


Expand Down Expand Up @@ -83,6 +80,10 @@ class BrowserTabs(val client: CefClient, val main: UltreonBrowser) : JTabbedPane
return tabs.indexOf(browserTab)
}

fun savePage() {
selected.savePage()
}

inner class CloseButtonHandler : ActionListener {
override fun actionPerformed(evt: ActionEvent) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ultreon.browser.main
package com.ultreon.browser

import com.ultreon.browser.dialog.ProgressDialog
import me.friwi.jcefmaven.EnumProgress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ultreon.browser.main
package com.ultreon.browser

import com.google.gson.annotations.SerializedName
import org.jetbrains.annotations.ApiStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ultreon.browser.main
package com.ultreon.browser

object Settings {
lateinit var theme: String
Expand Down
Loading

0 comments on commit 502e68b

Please sign in to comment.