trackwebview.vala 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /********************************************************************
  2. # Copyright 2014 Daniel 'grindhold' Brendle
  3. #
  4. # This file is part of Rainbow Lollipop.
  5. #
  6. # Rainbow Lollipop is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation, either
  9. # version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # Rainbow Lollipop is distributed in the hope that it will be
  13. # useful, but WITHOUT ANY WARRANTY; without even the implied
  14. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. # PURPOSE. See the GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public
  18. # License along with Rainbow Lollipop.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. namespace RainbowLollipop {
  22. /**
  23. * Extended Version of WebKit's WebView which is able to store
  24. * a reference to a HistoryTrack along with it and offers methods
  25. * to communicate with the web-extension-process of this view.
  26. */
  27. public class TrackWebView : WebKit.WebView {
  28. /**
  29. * Stores a reference to the HistoryTrack that is associated with
  30. * this WebView
  31. */
  32. public weak HistoryTrack track{ get;set; }
  33. private SearchWidget search;
  34. private bool searchstate;
  35. private string? _last_link = "";
  36. public string? last_link {get { return this._last_link; }}
  37. /**
  38. * Creates a new WebView for rainbow-lollipop
  39. */
  40. public TrackWebView(Clutter.Actor webactor) {
  41. this.search = new SearchWidget(webactor, this.get_find_controller());
  42. webactor.add_child(this.search);
  43. this.button_press_event.connect(do_button_press_event);
  44. this.mouse_target_changed.connect(do_mouse_target_changed);
  45. this.notify["is-playing-audio"].connect(do_update_playing_audio);
  46. }
  47. /**
  48. * Remembers the last button that has been pressed down on the mouse
  49. * This is used to determine wheter a link has been clicked with a
  50. * left mousebutton or with the middle mousebutton, so we can open
  51. * links as new nodes.
  52. *
  53. * Handles mouse events that we need to override webkits default behaviour
  54. * for. At the moment these are:
  55. * - Links being clicked with the middle mousebutton
  56. * - Links being clicked regularly with the left mousebutton
  57. * TODO: find out if this is really necessary some time. It works and
  58. * fixes issue #57 but i find it a bit cumbersome
  59. */
  60. public bool do_button_press_event(Gdk.EventButton e) {
  61. if (e.button == Gdk.BUTTON_MIDDLE && this._last_link != null) {
  62. this.track.log_call(this._last_link, false);
  63. return true;
  64. }
  65. else if (e.button == Gdk.BUTTON_PRIMARY && this._last_link != null
  66. && this._last_link != "") {
  67. this.load_uri(this._last_link);
  68. return true;
  69. }
  70. return false;
  71. }
  72. /**
  73. * Remembers the last link that the user hovered over
  74. */
  75. public void do_mouse_target_changed(WebKit.HitTestResult htr, uint modifiers) {
  76. this._last_link = htr.link_uri;
  77. }
  78. /**
  79. * Displays an icon over the current node of this track when audio is
  80. * played.
  81. */
  82. public void do_update_playing_audio() {
  83. this.track.set_audio_playing(this.is_playing_audio);
  84. }
  85. /**
  86. * Asks The web-extension-process whether this webview currently
  87. * needs input from the keyboard e.g. to fill content into a
  88. * HTML form-element.
  89. */
  90. public async void needs_direct_input(IPCCallback cb, Gdk.EventKey e) {
  91. ZMQVent.needs_direct_input(this, cb, e);
  92. }
  93. /**
  94. * Asks the web-extension for information about the scroll position of the
  95. * current_page
  96. */
  97. public async void get_scroll_info(IPCCallback cb) {
  98. ZMQVent.get_scroll_info(this,cb);
  99. }
  100. /**
  101. * Tells the webviews webprocess to scroll to the given positionö
  102. */
  103. public async void set_scroll_info(long x, long y) {
  104. ZMQVent.set_scroll_info(this, x, y);
  105. }
  106. /**
  107. * Returns true if the searchoverlay is currently active for this webview
  108. */
  109. public bool is_search_active() {
  110. return this.searchstate;
  111. }
  112. /**
  113. * Shows up the search dialog
  114. */
  115. public void start_search(string search_string="") {
  116. this.search.visible = this.searchstate = true;
  117. this.search.set_search_string(search_string);
  118. }
  119. /**
  120. * Returns the current search string
  121. */
  122. public string get_search_string() {
  123. return this.search.get_search_string();
  124. }
  125. /**
  126. * Shuts down the search overlay
  127. */
  128. public void stop_search() {
  129. this.searchstate = false;
  130. }
  131. /**
  132. * Hides the search overlay
  133. */
  134. public void hide_search() {
  135. this.search.visible = false;
  136. }
  137. /**
  138. * Shows the search overlay if there is currently a search going on
  139. */
  140. public void restore_search() {
  141. this.search.visible = this.searchstate;
  142. }
  143. }
  144. }