Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrolling app window feature added #49

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 36 additions & 3 deletions src/Assistant.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import re
from datetime import datetime

import threading
import Support
from VoiceInterface import VoiceInterface

LISTENING_ERROR = "Say that again please..."


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two line gap is enough...no need for having three blank lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept only two blank lines.


class Assistant:
def __init__(self):
"""Creates an Assistant instance consisting of an VoiceInterface instance"""
self.__voiceInterface = VoiceInterface()



def wish_user(self):
"""Wishes user based on the hour of the day"""
Expand Down Expand Up @@ -40,15 +43,17 @@ def listen_for_query(self) -> str:


def execute_query(self, query: str) -> None:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the lines

"""Processes the query string and runs the corresponding tasks

Args:
query (str): the query string obtained from speech input
"""
# if any( text in query for text in ['exit', 'quit', 'close'] ):
# return

if 'what can you do' in query:
if query is None:
print("No query detected. Please provide an input.")
elif 'what can you do' in query:
Support.explain_features(self.__voiceInterface)

elif re.match(r'(what|all|list).*apps.*open', query):
Expand Down Expand Up @@ -78,6 +83,34 @@ def execute_query(self, query: str) -> None:

elif any(text in query for text in ["the time", "time please"]):
Support.tell_time(self.__voiceInterface)

elif 'scroll' in query:
if re.search(r'start scrolling (up|down|left|right|top|bottom)', query):
direction = re.findall(r'start scrolling (up|down|left|right|top|bottom)', query)[0]
scroll_thread,stop_scroll_event=Support.setup_scrolling()
if scroll_thread is None: # Only start if not already scrolling
Support.start_scrolling(direction)

elif 'stop scrolling' in query:
scroll_thread,stop_scroll_event=Support.setup_scrolling()
if scroll_thread is not None:
Support.stop_scrolling()


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary gap between two elif blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the blank lines. I also went the through the Support.py file then identified and removed some redundant/blank lines too.



elif re.search(r'scroll to (up|down|left|right|top|bottom)', query):
match = re.search(r'scroll to (up|down|left|right|top|bottom)', query)
direction = match.group(1)
Support.scroll_to(direction)
elif re.search(r'scroll (up|down|left|right)', query):
match = re.search(r'scroll (up|down|left|right)', query)
direction = match.group(1)
Support.simple_scroll(direction)


else:
print("Scroll command not recognized")

else:
self.__voiceInterface.speak("could not interpret the query")
Expand Down
117 changes: 115 additions & 2 deletions src/Support.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import os
from datetime import datetime
import threading
import time
import pyautogui as pag
import pygetwindow as gw
from PIL import ImageGrab

import googlesearch
import wikipedia

from ExternalPaths import AppPath, WebPath, features
from VoiceInterface import VoiceInterface

#include the actual code to gradual score
scroll_thread = None
stop_scroll_event = threading.Event()

def clear_screen():
if os.name == "posix":
Expand Down Expand Up @@ -120,4 +127,110 @@ def tell_time(vi: VoiceInterface) -> None:
hour, minute, second = date_time.hour, date_time.minute, date_time.second
tmz = date_time.tzname()

vi.speak(f"Current time is {hour}:{minute}:{second} {tmz}")
vi.speak(f"Current time is {hour}:{minute}:{second} {tmz}")

def setup_scrolling():
if not hasattr(setup_scrolling, "scroll_thread"):
setup_scrolling.scroll_thread = None
if not hasattr(setup_scrolling, "stop_scroll_event"):
setup_scrolling.stop_scroll_event = threading.Event()

return setup_scrolling.scroll_thread, setup_scrolling.stop_scroll_event

def start_gradual_scroll(direction: str, stop_event: threading.Event) -> None:
"""Gradually scroll in the given direction until stop_event is set."""
time.sleep(2)
active_window = pag.getActiveWindow()
if active_window:

left, top, width, height = active_window.left, active_window.top, active_window.width, active_window.height


previous_image = ImageGrab.grab(bbox=(left, top, left + width, top + height)) # Capture the entire window


while True:
if stop_event.is_set():

break
pag.press(direction)
time.sleep(1)

current_image = ImageGrab.grab(bbox=(left, top, left + width, top + height))

if list(current_image.getdata()) == list(previous_image.getdata()):
print("Reached to extreme")
stop_event.set()
setup_scrolling.scroll_thread = None
break

previous_image = current_image





print(f"Scrolling {direction}...") # Simulate scrolling action
# Simulate delay between scroll actions
print(f"Stopped scrolling {direction}.")

def start_scrolling(direction: str) -> None:
"""Start a new scroll thread."""

setup_scrolling.stop_scroll_event.clear()
setup_scrolling.scroll_thread = threading.Thread(target=start_gradual_scroll, args=(direction, setup_scrolling.stop_scroll_event))
setup_scrolling.scroll_thread.start()

def stop_scrolling() -> None:
"""Stop the current scrolling thread."""

setup_scrolling.stop_scroll_event.set()
if setup_scrolling.scroll_thread is not None:
setup_scrolling.scroll_thread.join()
setup_scrolling.scroll_thread = None
print("Scrolling has stopped.")


def scroll_to(direction:str)->None:
active_window = gw.getActiveWindow()
if active_window:
# Bring the active window to the front
active_window.activate()
time.sleep(0.5)
if direction=='top':
pag.press('home')

elif direction=='bottom':
pag.press('end')

elif direction=='right':
pag.press('right', presses=9999)

elif direction=='left':
pag.press('left', presses=9999)

else:
print("Invalid Command")


#pygetwindow and implement
def simple_scroll(direction:str)->None:
active_window = gw.getActiveWindow()
if active_window:
# Bring the active window to the front
active_window.activate()
time.sleep(0.5)
if direction=='up':
pag.press('up', presses=100)
elif direction=='down':
pag.press('down', presses=100)
elif direction=='right':
pag.press('right', presses=500)

elif direction=='left':
pag.press('left', presses=500)

else:
print("Invalid direction")


2 changes: 1 addition & 1 deletion src/VoiceInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def listen(self, print_statement: bool = False) -> Any | None:
try:
# language = English(en)-India(in)
if print_statement: print("Recognizing...\n")
query = self.__recognizer.recoginize_google(audio_data=audio, language="en-in")
query = self.__recognizer.recognize_google(audio_data=audio, language="en-in")
return query
except:
return None
Expand Down
Loading