"""
MIT License

Copyright (c) 2021 <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys

from pystray import MenuItem as Item
import pystray
from PIL import Image, ImageTk
from LoopThread import Loop
import tkinter as tk
from tkinter import PhotoImage as tkimg


def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


class NoAFK():
    def __init__(self):
        self._async_loop = None
        self._delay = 360
        self._key = 0x11 #0x27 right arow #0x70 f1 https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
        self._percent = 0.5
        self._image = Image.open(resource_path("NOAFK.ico"))
        menu = pystray.Menu(Item(text="XIV AFK Go Away", action=None, enabled=False, default=True),
                            Item("Edit Details", self.show_window),
                            Item("Invoke", self.invoke),
                            Item("Quit", self.quit))
        self._icon = pystray.Icon(name="XIV AFK Go Away", icon=self._image, title="AFK Go Away", menu=menu)
        self._icon.HAS_DEFAULT_ACTION = False
        self._start_loop(self._delay, self._key, self._percent)
        self._window = None
        self._icon.run()
        self._end_loop()

    def __del__(self):
        self._end_loop()

    def _start_loop(self, delay, key, rand_percent):
        if self._async_loop is not None:
            self._end_loop()
        self._async_loop = Loop(delay, key, rand_percent)
        self._async_loop.start()

    def _end_loop(self):
        if self._async_loop is not None:
            self._async_loop.send_end()
        self._async_loop = None

    def close_tkinter(self):
        if self._window is not None:
            self._window.withdraw()

    def invoke(self):
        self._async_loop.invoke()

    # Define a function for quit the window
    def quit(self):
        if self._window is not None:
            self._window.destroy()
        self._end_loop()
        self._icon.stop()

    # Define a function to show the window again
    def show_window(self):
        if self._window is None:
            self._window = tk.Tk()
            self._window.title("Hi")
            self._window.protocol('WM_DELETE_WINDOW', self.close_tkinter)
            self._window.iconphoto(False, ImageTk.PhotoImage(self._image))
            self._window.mainloop()
        else:
            self._window.deiconify()


if __name__ == '__main__':
    NoAFK()

"""
MIT License

Copyright (c) 2021 <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys

from pystray import MenuItem as Item
import pystray
from PIL import Image, ImageTk
from LoopThread import Loop
import tkinter as tk
from tkinter import PhotoImage as tkimg


def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


class NoAFK():
    def __init__(self):
        self._async_loop = None
        self._delay = 360
        self._key = 0x11 #0x27 right arow #0x70 f1 https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
        self._percent = 0.5
        self._image = Image.open(resource_path("NOAFK.ico"))
        menu = pystray.Menu(Item(text="XIV AFK Go Away", action=None, enabled=False, default=True),
                            Item("Edit Details", self.show_window),
                            Item("Invoke", self.invoke),
                            Item("Quit", self.quit))
        self._icon = pystray.Icon(name="XIV AFK Go Away", icon=self._image, title="AFK Go Away", menu=menu)
        self._icon.HAS_DEFAULT_ACTION = False
        self._start_loop(self._delay, self._key, self._percent)
        self._window = None
        self._icon.run()
        self._end_loop()

    def __del__(self):
        self._end_loop()

    def _start_loop(self, delay, key, rand_percent):
        if self._async_loop is not None:
            self._end_loop()
        self._async_loop = Loop(delay, key, rand_percent)
        self._async_loop.start()

    def _end_loop(self):
        if self._async_loop is not None:
            self._async_loop.send_end()
        self._async_loop = None

    def close_tkinter(self):
        if self._window is not None:
            self._window.withdraw()

    def invoke(self):
        self._async_loop.invoke()

    # Define a function for quit the window
    def quit(self):
        if self._window is not None:
            self._window.destroy()
        self._end_loop()
        self._icon.stop()

    # Define a function to show the window again
    def show_window(self):
        if self._window is None:
            self._window = tk.Tk()
            self._window.title("Hi")
            self._window.protocol('WM_DELETE_WINDOW', self.close_tkinter)
            self._window.iconphoto(False, ImageTk.PhotoImage(self._image))
            self._window.mainloop()
        else:
            self._window.deiconify()


if __name__ == '__main__':
    NoAFK()

MIT License  Copyright (c) 2021 <NAME>  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Get absolute path to resource, works for dev and for PyInstaller # PyInstaller creates a temp folder and stores path in _MEIPASS #0x27 right arow #0x70 f1 https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes # Define a function for quit the window # Define a function to show the window again