Skip to content

Commit

Permalink
Basic tray icon working.
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSpanel committed Sep 7, 2017
1 parent 6141ccf commit 8c00633
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
Binary file added push-uploader/src/main/resources/stravaUpload16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import akka.stream.{ActorMaterializer, BindFailedException}
import akka.util.ByteString

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future, duration}
import scala.concurrent.{Await, Future, Promise, duration}
import scala.util.Try
import scala.xml.Elem
import org.joda.time.{DateTimeZone, DateTime => ZonedDateTime}
Expand Down Expand Up @@ -111,6 +111,73 @@ object Start extends App {
useLocal
}

private object Tray {
import java.awt.{SystemTray, TrayIcon, AWTException, Image}
import javax.swing.SwingUtilities

private def showImpl() = {
assert(SwingUtilities.isEventDispatchThread)
import javax.imageio.ImageIO
// https://docs.oracle.com/javase/7/docs/api/java/awt/SystemTray.html

if (SystemTray.isSupported) {
val tray = SystemTray.getSystemTray
val iconSize = tray.getTrayIconSize
val imageFile = if ((iconSize.height max iconSize.width) > 16) "/stravaUpload32.png" else "/stravaUpload16.png"
val is = getClass.getResourceAsStream(imageFile)

val image = ImageIO.read(is)
val imageSized = image.getScaledInstance(iconSize.width, iconSize.height, Image.SCALE_SMOOTH)

val trayIcon = new TrayIcon(imageSized, "Stravamat")
try {
tray.add(trayIcon)
} catch {
case e: AWTException =>
e.printStackTrace()
}
Some(trayIcon)
} else {
None
}
}

private def removeImpl(icon: TrayIcon): Unit = {
assert(SwingUtilities.isEventDispatchThread)
if (SystemTray.isSupported) {
val tray = SystemTray.getSystemTray
tray.remove(icon)
}
}

private def changeStateImpl(icon: TrayIcon, state: =>String): Unit = {
assert(SwingUtilities.isEventDispatchThread)
val title = "Stravamat"
val text = if (state.isEmpty) title else title + ": " + state
icon.setToolTip(text)
}

def show(): Option[TrayIcon] = {
val p = Promise[Option[TrayIcon]]
SwingUtilities.invokeLater(new Runnable {
override def run() = p.success(showImpl())
})
Await.result(p.future, Duration.Inf)
}

def remove(icon: TrayIcon): Unit = {
SwingUtilities.invokeLater(new Runnable {
override def run() = removeImpl(icon)
})
}

def changeState(icon: TrayIcon, state: =>String): Unit = {
SwingUtilities.invokeLater(new Runnable {
override def run() = changeStateImpl(icon, state)
})
}
}

private def startBrowser() = {
/*
Authentication dance
Expand Down Expand Up @@ -213,6 +280,8 @@ object Start extends App {
// sort files by timestamp
val wantedFiles = listFiles.filter(MoveslinkFiles.timestampFromName(_).forall(_ > sinceDate))

reportProgress(wantedFiles.size)

val sortedFiles = wantedFiles.sortBy(MoveslinkFiles.timestampFromName)

val localTimeZone = DateTimeZone.getDefault.toString
Expand Down Expand Up @@ -291,17 +360,27 @@ object Start extends App {
f -> req
}

reportProgress(reqs.size)

reqs.foreach { case (f, r) =>
reqs.zipWithIndex.foreach { case ((f, r), i) =>
Await.result(r, Duration.Inf)
// TODO: handle upload failures somehow
println(s" Await upload $f status $r")
reportProgress(reqs.size + 1 - i)
}

serverInfo.system.terminate()

}

val icon = Tray.show()

def reportProgress(i: Int): Unit = {
def localSuffix = if (useLocal) " to local server" else ""
def state = s"Uploading $i files" + localSuffix
icon.foreach(Tray.changeState(_, state))
}

checkLocalStravamat()

private val serverInfo = startHttpServer(serverPort)
Expand All @@ -318,5 +397,5 @@ object Start extends App {

Await.result(serverInfo.system.whenTerminated, Duration.Inf)
println("System stopped")

icon.foreach(Tray.remove)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ abstract class DefineRequest(val handleUri: String, val method: Method = Method.
<table>
<tr>
<td>
<a href="/"><img src="static/stravaUpload32.png"></img></a>
<a href="/"><img src="static/stravaUpload64.png"></img></a>
</td>
<td>
<table>
Expand Down
File renamed without changes
Binary file modified work/stravaUpload32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added work/stravaUpload64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8c00633

Please sign in to comment.