Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Added few notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
koral committed Apr 3, 2012
1 parent 81b7ddc commit fc85aad
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
30 changes: 18 additions & 12 deletions Hbro/Config.hs
Expand Up @@ -59,7 +59,7 @@ defaultConfig = Config {
defaultHooks :: Hooks
defaultHooks = Hooks {
mBackForward = (\_ decision -> io $ webPolicyDecisionUse decision),
mDownload = (\_ _ _ -> return ()),
mDownload = defaultDownloadHook,
mFormResubmitted = (\_ decision -> io $ webPolicyDecisionUse decision),
mFormSubmitted = (\_ decision -> io $ webPolicyDecisionUse decision),
mKeyPressed = void . (defaultKeyHandler defaultKeyBindings),
Expand All @@ -73,13 +73,19 @@ defaultHooks = Hooks {
mTitleChanged = defaultTitleChangedHook
}

-- | Warn user about missing download hook.
defaultDownloadHook :: URI -> String -> Int -> K ()
defaultDownloadHook _ _ _ = notify 5000 "No download hook defined."

-- | Look for a callback associated to the given keystrokes and trigger it, if any.
defaultKeyHandler :: KeysList -> String -> K (String, Bool)
defaultKeyHandler keysList keystrokes = do
io . whenLoud . putStrLn . ("Key pressed: " ++) $ keystrokes
case M.lookup keystrokes (M.fromList keysList) of
Just callback -> (io . whenLoud . putStrLn $ " (mapped)") >> callback >> return (keystrokes, True)
_ -> (io . whenLoud . putStrLn $ " (unmapped)") >> return (keystrokes, False)
io . whenLoud . putStr $ "Key pressed: " ++ keystrokes
(log', isMapped) <- case M.lookup keystrokes (M.fromList keysList) of
Just callback -> callback >> return (" (mapped)", True)
_ -> return (" (unmapped)", False)
io . whenLoud . putStrLn $ log'
return (keystrokes, isMapped)

-- | Default key bindings.
defaultKeyBindings :: KeysList
Expand All @@ -91,7 +97,7 @@ defaultKeyBindings = [
("<F5>", reload),
("C-r", reload),
("C-<F5>", reloadBypassCache),
("C-R", reloadBypassCache),
("M-r", reloadBypassCache),
("C-^", scroll Horizontal (Absolute 0)),
("C-$", scroll Horizontal (Absolute 100)),
("C-<Home>", scroll Vertical (Absolute 0)),
Expand All @@ -108,13 +114,13 @@ defaultKeyBindings = [
("C-o", Prompt.readURI "Open URI" [] loadURI),
("M-o", withURI $ \uri -> Prompt.readURI "Open URI " (show uri) loadURI),
-- Search
("/", Prompt.iread "Search " [] $ searchText False True True >=> const (return ())),
("C-f", Prompt.iread "Search " [] $ searchText False True True >=> const (return ())),
("?", Prompt.iread "Search " [] $ searchText False False True >=> const (return ())),
("C-n", withK (mEntry . mPromptBar . mGUI) $ (io . entryGetText) >=> searchText False True True >=> const (return ())),
("C-N", withK (mEntry . mPromptBar . mGUI) $ (io . entryGetText) >=> searchText False False True >=> const (return ())),
("/", Prompt.iread "Search " [] $ void . searchText CaseInsensitive Forward Wrap),
("C-f", Prompt.iread "Search " [] $ void . searchText CaseInsensitive Forward Wrap),
("?", Prompt.iread "Search " [] $ void . searchText CaseInsensitive Backward Wrap),
("C-n", withK (mEntry . mPromptBar . mGUI) $ (io . entryGetText) >=> void . searchText CaseInsensitive Forward Wrap),
("C-N", withK (mEntry . mPromptBar . mGUI) $ (io . entryGetText) >=> void . searchText CaseInsensitive Backward Wrap),
-- Misc
("<Escape>", with (mBox . mPromptBar . mGUI) widgetHide), -- DUPE !
("<Escape>", with (mBox . mPromptBar . mGUI) widgetHide),
("C-i", showWebInspector),
("C-p", printPage),
("C-t", io $ spawn "hbro" []),
Expand Down
16 changes: 12 additions & 4 deletions Hbro/Core.hs
Expand Up @@ -117,8 +117,15 @@ getState key defaultValue = with mState $ \state -> do

-- {{{ Browsing
goBack, goForward, goHome :: K ()
goBack = with (mWebView . mGUI) webViewGoBack
goForward = with (mWebView . mGUI) webViewGoForward
goBack = withK (mWebView . mGUI) $ \view -> do
canGoBack <- io . webViewCanGoBack $ view
unless canGoBack $ notify 5000 "Cannot go back anymore"
io . webViewGoBack $ view
goForward = withK (mWebView . mGUI) $ \view -> do
canGoForward <- io . webViewCanGoForward $ view
unless canGoForward $ notify 5000 "Cannot go forward anymore"
io . webViewGoForward $ view

goHome = withK (mHomePage . mConfig) $ mapM_ loadURI . parseURIReference

loadURI :: URI -> K ()
Expand Down Expand Up @@ -181,8 +188,9 @@ notify duration text = with (mNotificationBar . mGUI) $ \notificationBar -> do
modifyIORef timer $ const . Just $ newID

-- | Wrapper around webViewSearchText, provided for convenience
searchText :: Bool -> Bool -> Bool -> String -> K Bool
searchText a b c text = with (mWebView . mGUI) $ \view -> webViewSearchText view text a b c
searchText :: CaseSensitivity -> Direction -> Wrap -> String -> K Bool
searchText s d w text = with (mWebView . mGUI) $ \view ->
webViewSearchText view text (isCaseSensitive s) (isForward d) (isWrapped w)

-- | Toggle source display.
-- Current implementation forces a refresh of current web page, which may be undesired.
Expand Down
2 changes: 1 addition & 1 deletion Hbro/Hbro.hs
Expand Up @@ -188,7 +188,7 @@ onDownload environment download = do
(Just uri', Just filename') -> do
whenLoud . putStrLn . ("Requested download: " ++) . show $ uri'
runK environment $ do
notify 5000 . ("Downloading " ++) . show $ uri'
notify 5000 $ "Requested download: " ++ filename' ++ " (" ++ show size ++ ")"
callback uri' filename' size
_ -> return ()
return False
Expand Down
5 changes: 5 additions & 0 deletions Hbro/Types.hs
Expand Up @@ -142,3 +142,8 @@ type CommandsMap = Map String ([String] -> K String)

-- |
data Button = ButtonL | ButtonM | ButtonR

-- Boolean datatypes
data CaseSensitivity = CaseSensitive | CaseInsensitive
data Direction = Forward | Backward
data Wrap = Wrap | NoWrap
17 changes: 17 additions & 0 deletions Hbro/Util.hs
Expand Up @@ -4,6 +4,10 @@ module Hbro.Util (
-- * Process management
spawn,
getAllProcessIDs,
-- * Boolean data-types conversion
isCaseSensitive,
isForward,
isWrapped,
-- * Misc
send'',
labelSetMarkupTemporary,
Expand Down Expand Up @@ -105,3 +109,16 @@ errorHandler file e = do
when (isAlreadyInUseError e) $ (whenNormal . putStrLn) ("ERROR: file <" ++ file ++ "> is already opened and cannot be reopened.")
when (isDoesNotExistError e) $ (whenNormal . putStrLn) ("ERROR: file <" ++ file ++ "> doesn't exist.")
when (isPermissionError e) $ (whenNormal . putStrLn) ("ERROR: user doesn't have permission to open file <" ++ file ++ ">.")

-- Boolean types conversion
isCaseSensitive :: CaseSensitivity -> Bool
isCaseSensitive CaseSensitive = True
isCaseSensitive _ = False

isForward :: Direction -> Bool
isForward Forward = True
isForward _ = False

isWrapped :: Wrap -> Bool
isWrapped Wrap = True
isWrapped _ = False

0 comments on commit fc85aad

Please sign in to comment.